dtufl26404 2016-02-27 09:22
浏览 33
已采纳

使用php循环多维数组以生成表列和行

I have the following array which came up from my two days work with arrays.

Array (
      [0]=> Array(
            [6]=>Array(
                  [English] => 17
                  [Maths] => 11
                  [Science] => 15
                  )
              )
      [1] =>Array(
            [7]=>Array(
                  [English] => 13
                  [Maths] => 15
                  [Science] => 11
                  )
             )
      [2] =>Array(
            [8]=>Array(
                  [English] => 9
                  [Maths] => 17
                  [Science] => 9
                  )
             )
     )

Here array key 6,7, and 8 are unique. I want to print out this array in this format.

<table border="1">
  <thead>
    <tr>
      <th>Roll No</th>
      <th>English</th>
      <th>Maths</th>
      <th>Science</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>6</td>
      <td>17</td>
      <td>11</td>
      <td>15</td>
    </tr>
    <tr>
      <td>7</td>
      <td>13</td>
      <td>15</td>
      <td>11</td>
    </tr>
    <tr>
      <td>8</td>
      <td>9</td>
      <td>17</td>
      <td>9</td>
    </tr>
  </tbody>
  </table>

Note: the header and the rows are dynamic.

</div>
  • 写回答

3条回答 默认 最新

  • dtsps2098 2016-02-27 10:54
    关注

    Given this array structure (taken from your question, put into code):

    $rows = array(
        array(
            6 => array(
                'English' => 17,
                'Maths' => 11,
                'Science' => 15
            )
        ),
        array(
            7 => array(
                'English' => 13,
                'Maths' => 15,
                'Science' => 11
            )
        ),
        array(
            8 => array(
                'English' => 9,
                'Maths' => 17,
                'Science' => 9
            )
        )
    );
    

    First, get all the subjects (English, Maths, Science, etc) that need to be displayed:

    $columns = array();
    
    foreach ($rows as $row) {
        foreach ($row as $roll => $set) {
            foreach ($set as $class => $score) {
                $columns[$class] = $class;
            }
        }
    }
    

    Then, display them (quick and dirty):

    <table border="1">
        <thead>
            <tr>
                <th>Roll No</th>
            <?php
                foreach ($columns as $column) {
                    echo '<th>' . $column . '</th>';
                }
            ?>
            </tr>
        </thead>
        <tbody>
        <?php
            foreach ($rows as $row) {
                echo '<tr>';
                foreach ($row as $roll => $set) {
                    echo '<td>' . $roll . '</td>';
                    foreach ($columns as $class) {
                        echo '<td>' . ((array_key_exists($class, $set)) ? $set[$class] : 'n/a') . '</td>';
                    }
                }
                echo '</tr>';
            }
        ?>
        </tbody>
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题