0

我刚刚问了一个关于关联数组和使用 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  |
4

3 回答 3

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>
于 2012-11-08T12:35:45.977 回答
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>";
         }
    }
    ?>
于 2012-11-08T12:37:08.303 回答
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>';
}
于 2012-11-08T12:53:11.763 回答