简体   繁体   中英

Looping two arrays to make html table in php

I have two array, one is meant for table heading and second is meant for table data.

Table Heading array

  Array
  (
   [0] => techrepublic.com
   [1] => office.microsoft.com
   [2] => en.pstrecovery.net
   [3] => easeus.com
   [4] => download.cnet.com
   [5] => datanumen.com
   [6] => 2324
 )

Table Data array

  Array
(
   [software services] => Array
    (
        [datanumen.com] => 2
        [office.microsoft.com] => 3
        [easeus.com] => 8
        [download.cnet.com] => 9
        [en.pstrecovery.net] => 10
    )

[software services in Delhi] => Array
    (
        [en.pstrecovery.net] => 1
        [datanumen.com] => 3
        [office.microsoft.com] => 4
        [easeus.com] => 9
        [download.cnet.com] => 10
    )

[orignal software services] => Array
    (
        [en.pstrecovery.net] => 1
        [easeus.com] => 6
        [download.cnet.com] => 7
        [datanumen.com] => 8
        [office.microsoft.com] => 9
    )

)

I want output table as shown in attachment 在此处输入图片说明

What i have done till now

    // code for table heading
    foreach ( $_POST['competitor']) as $value) { // $_POST['competitor']) 
                                                // containts table heading 
                                                // array
          echo "<th>". $value ."</th>"  ;
    }

  //code for table row 

     foreach ($details as $Keywords => $comp) {  // $details contains row
                                    // data array
       echo '<tr><td>' .$Keywords .  '</td>';
       foreach ($_POST['competitor'] as $competitor) {
        if (isset($comp[$competitor])) {
             echo '<td>' . $comp[$competitor] . '</td>' ;
        } else {
        echo '<td>Not Found</td>' ;
       }
      }
       echo '</tr>' ;
      }

I am unable to arrange td and tr. How can I achieve the output as shown in attachment?

Currently i am getting following output which is wrong : 在此处输入图片说明

The thing is, in order to get the data in the same order as the headers, you need to loop through the headers again for each row in the data. Since your data uses the headers as array keys, this should be pretty easy.

foreach ( $_POST['competitor'] as $value) {
    echo "<th>". htmlspecialchars($value) ."</th>"  ;
}

foreach ($details as $Keywords => $comp) {
    echo '<tr><td>' . htmlspecialchars($Keywords) .  '</td>';


    // iterate the headers here instead
    foreach ($_POST['competitor'] as $competitor) {

        // echo the value for that header from the current row if it's present
        if (isset($comp[$competitor])) {
            echo '<td>' . htmlspecialchars($comp[$competitor]) . '</td>' ;
        } else {
            echo '<td>Not Found</td>' ;
        }
    }
    echo '</tr>' ;
}

Side note - don't forget to escape your strings properly for HTML output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM