简体   繁体   English

循环两个数组以在php中创建html表

[英]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. 我无法安排td和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. 旁注-不要忘记为HTML输出正确地转义字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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