PHP - 最有效的方法为这个数组结构构建表

时间:2012-11-08 12:25:09

标签: php arrays

我刚问了一个关于关联数组的问题,并使用foreach来检索数据,但我正在努力想办法从这个数据结构构建我想要的表。

我有一个

形式的数组$dailytotals
Array
(
    [204] => Array
    (
        [1] => Array
           (
               [leads] => 9
           )

        [2] => Array
            (
                [leads] => 15
            )

    )

    [200] => Array
    (
        [1] => Array
            (
                [leads] => 7
            )

        [2] => Array
            (
                [leads] => 16
            )

        [3] => Array
            (
                [leads] => 5
            )
)

所以我可以在主数组中包含任意数量的子数组,其中包含任意数量的子数组。

到目前为止,我已经完成了构建表头的重大任务:

<table>
    <tr>
        <th>Clinic</th>

        <?php
         // get unique columns - not all clinics will have leads for all columns
        $columns = array();
        foreach ($dailytotals as $key => $arr) {
            $columns = array_unique(array_merge($columns, array_keys($arr)));
        }

        foreach ($columns as $index => $campaignid) {
           echo '<th>' . $campaignid . '</th>';
        }
       ?>
    </tr>

但我现在完全陷入了如何构建表体的过程。

我想要构建的结构是:

Clinic  |  1  |  2  |  3  |
___________________________
204     |  9  |  15 |  0  |
200     |  7  |  16 |  5  |

3 个答案:

答案 0 :(得分:1)

您需要一些嵌套循环。试试这个:

<table>
    <tr>
        <th>Clinic</th>

        <?php
         // get unique columns - not all clinics will have leads for all columns
        $columns = array();
        foreach ($dailytotals as $key => $arr) {
            $columns = array_unique(array_merge($columns, array_keys($arr)));
        }

        foreach ($columns as $index => $campaignid) {
           echo '<th>' . $campaignid . '</th>';
        }
       ?>
    </tr>
    <?php
        foreach($dailytotals as $clinic => $data)
        {
            echo '<tr>';
            echo '<td>'.$clinic.'</td>';
            foreach($columns as $column)
            {
                echo '<td>';
                echo isset($data[$column]) ? $data[$column]['leads'] : 0;
                echo '</td>';
            }
            echo '</tr>';
        }

    ?>
</table>

答案 1 :(得分:1)

此代码未经过测试,但只需稍加修改即可尝试(如果需要)

 <table>
  <tr>
    <th>Clinic</th>
  <?php
     // get unique columns - not all clinics will have leads for all columns
    $columns = array();
    $max=0;
    foreach ($dailytotals as $key => $arr) {
        $columns = array_unique(array_merge($columns, array_keys($arr)));
    }

    foreach ($columns as $index => $campaignid) {
       echo '<th>' . $campaignid . '</th>';
       $max=$campaignid;
    }
    ?>
  </tr>

   <?php
    $columns = array();
    foreach ($dailytotals as $key => $arr) {
        for($i=0;$i< $max;$++)
         {
            echo "<tr>";
            if(is_set($arr[i])
             {
                echo "<td>".$arr[i]."</td>";
             }
            else
             {
                echo "<td>0</td>";
             }
            echo "</tr>";
         }
    }
    ?>

答案 2 :(得分:1)

试试这个

foreach ($dailytotals as $key => $arr) {
  echo '<tr>';
  foreach ($columns as $campaignid) {
    $val = isset($arr[$campaignid]) ? $arr[$campaignid]['leads'] : 0;
    echo '<td>' . $val . '</td>';
  }
  echo '</tr>';
}