[英]Looping Multidimensional array to generate table columns and rows using php
我在使用数组的两天中得到了以下数组。
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>
注意:标题和行是动态的。
请尝试一下
<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次,第一次运行生成<tr>
3次,第二次显示记录。
对于th
我们要roll number
静态的,因为我们没有强阵了点。
使用array_walk
, array_merge
, array_values
和array_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
给定此数组结构(从您的问题中取出,放入代码中):
$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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.