【问题标题】:PHP - Most Efficient Way to build table for this array structurePHP - 为这个数组结构构建表的最有效方法
【发布时间】:2012-11-08 12:25:09
【问题描述】:

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

【问题讨论】:

    标签: php arrays


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

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

       <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>";
               }
          }
          ?>
      

      【讨论】:

        【解决方案3】:

        试试这个

        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>';
        }
        

        【讨论】:

          猜你喜欢
          • 2013-09-17
          • 1970-01-01
          • 1970-01-01
          • 2017-04-25
          • 1970-01-01
          • 1970-01-01
          • 2022-10-23
          • 1970-01-01
          • 2017-02-03
          相关资源
          最近更新 更多