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

时间:2016-02-27 09:22:05

标签: php arrays multidimensional-array

我有两个数组来自我使用数组的两天工作。

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
                  )
             )
     )

这里数组键6,7和8是唯一的。我想以这种格式打印出这个数组。



<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>
&#13;
&#13;
&#13;

注意:标题和行是动态的。

3 个答案:

答案 0 :(得分:1)

请试试这个,

<table>
  <thead>
   <tr>
     <th>Roll No</th>
     <?php foreach($arrays as $k1=>$val1): ?>
       <?php foreach($val1 as $k2=>$val2): ?>
         <?php foreach($val2 as $k3=>$val3): ?>
           <th><?=$k3?></th>
         <?php endforeach; ?>
       <?php break; endforeach; ?>
     <?php break; endforeach; ?>
  </tr>
  </thead>
  <tbody>
    <?php foreach($arrays as $k1=>$val1): ?>
      <tr>
       <?php foreach($val1 as $k2=>$val2): ?>
        <th><?=$k2?></th>
        <th><?=$val2['English']?></th>
        <th><?=$val2['Maths']?></th>
        <th><?=$val2['Science']?></th>
       <?php endforeach; ?>
      </tr>
    <?php endforeach; ?>
  </tbody>
  </table>

由于你没有复杂的array,我们必须运行foreach 2次,第1次运行<tr> 3次,第2次运行以显示记录。

对于th,我们必须使roll number为静态,因为我们没有强大的数组。

答案 1 :(得分:1)

快速”具有array_walkarray_mergearray_valuesarray_keys功能的解决方案:

// assuming that $arr is your initial array
$header_sequence = ['Science','Maths','English']; // if header's key sequence is dynamic
sort($header_sequence);

$result = [];
array_walk($arr, function($v, $k) use (&$result){    
    $key = array_keys($v)[0];
    $result[$k] = array_merge([$key], array_values($v[$key]));    
});

?>
<table>
  <thead>
   <tr>
      <th>Roll No</th>
      <?php foreach($header_sequence as $item): ?>
         <th><?= $item ?></th>
      <?php endforeach; ?>
  </tr>
  </thead>
  <tbody>
    <?php foreach($result as $k => $v): ?>
      <tr>
       <?php foreach($v as $item): ?>
          <td><?= $item ?></td>
       <?php endforeach; ?>
      </tr>
    <?php endforeach; ?>
  </tbody>

输出:

Roll No English Maths Science
6       17      11    15
7       13      15    11
8       9       17    9

答案 2 :(得分:1)

给定这个数组结构(取自你的问题,放入代码中):

$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
        )
    )
);

首先,获取需要显示的所有科目(英语,数学,科学等):

$columns = array();

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

然后,显示它们(快速和脏):

<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>