我有一个名为$grouped_types
的数组。当我打印它时,显示如下:
Array
(
[XMU_1_1] => Array
(
[0] => Array
(
[port] => 1
[band] =>
[name] => XMU_1_1
)
[1] => Array
(
[port] => 2
[band] =>
[name] => XMU_1_1
)
[2] => Array
(
[port] => 3
[band] =>
[name] => XMU_1_1
)
[3] => Array
(
[port] => 4
[band] =>
[name] => XMU_1_1
)
[4] => Array
(
[port] => 5
[band] =>
[name] => XMU_1_1
)
[5] => Array
(
[port] => 6
[band] =>
[name] => XMU_1_1
)
[6] => Array
(
[port] => 7
[band] =>
[name] => XMU_1_1
)
[7] => Array
(
[port] => 8
[band] =>
[name] => XMU_1_1
)
[8] => Array
(
[port] => 9
[band] =>
[name] => XMU_1_1
)
[9] => Array
(
[port] => 10
[band] =>
[name] => XMU_1_1
)
[10] => Array
(
[port] => 11
[band] =>
[name] => XMU_1_1
)
[11] => Array
(
[port] => 12
[band] =>
[name] => XMU_1_1
)
[12] => Array
(
[port] => 13
[band] =>
[name] => XMU_1_1
)
[13] => Array
(
[port] => 14
[band] => 700
[name] => XMU_1_1
)
[14] => Array
(
[port] => 15
[band] =>
[name] => XMU_1_1
)
[15] => Array
(
[port] => 16
[band] =>
[name] => XMU_1_1
)
)
[DUS_1_1] => Array
(
[0] => Array
(
[port] => A
[band] =>
[name] => DUS_1_1
)
[1] => Array
(
[port] => B
[band] =>
[name] => DUS_1_1
)
[2] => Array
(
[port] => C
[band] =>
[name] => DUS_1_1
)
[3] => Array
(
[port] => D
[band] =>
[name] => DUS_1_1
)
[4] => Array
(
[port] => E
[band] =>
[name] => DUS_1_1
)
[5] => Array
(
[port] => F
[band] =>
[name] => DUS_1_1
)
)
[DUS_1_2] => Array
(
[0] => Array
(
[port] => A
[band] =>
[name] => DUS_1_2
)
[1] => Array
(
[port] => B
[band] =>
[name] => DUS_1_2
)
[2] => Array
(
[port] => C
[band] =>
[name] => DUS_1_2
)
[3] => Array
(
[port] => D
[band] =>
[name] => DUS_1_2
)
[4] => Array
(
[port] => E
[band] =>
[name] => DUS_1_2
)
[5] => Array
(
[port] => F
[band] =>
[name] => DUS_1_2
)
)
)
现在我希望这个数组在表格中显示如下:
XMU_1_1 port XMU_1_1 band
1
2
3
4
5
6
7
8
9
10
11
12
13
14 700
15
16
这只是我想要的每个不同名称的一个名称。每个名字都应该有不同的表格。如何获取我的数组,以便我可以为不同的表显示它?我尝试过,但我不知道如何获取这种类型的数组。
答案 0 :(得分:1)
循环遍历数组的最简单方法是使用foreach()
循环。使用多维数组,您必须有一个或两个嵌套foreach()
循环,如代码中所示片段。我不是说这正是适合你的代码;它只是为了说明嵌套的foreach()循环是什么样的。希望它可以帮助你解决问题
<?php
echo "<table>";
foreach ($grouped_types as $k => $val) {
echo "<tr>
<th>$k port </th>
<th>$k band </th>
</tr>";
if (is_array($val)) {
foreach ($val as $v) {
echo "<tr>
<td>" . $v['port'] . "</td>
<td>" . $v['band'] . "</td>
</tr>";
}
}
}
echo "</table>";
?>
<强>输出:强>
答案 1 :(得分:0)
根据您的数组,这将使用foreach()
循环按要求执行。
$array = [
"XMU_1_1" =>[
[
"port" => 1,
"band" => null,
"name" => "XMU_1_1",
],
[
"port" => 2,
"band" => null,
"name" => "XMU_1_1",
],
[
"port" => 3,
"band" => null,
"name" => "XMU_1_1",
],
[
"port" => 4,
"band" => 700,
"name" => "XMU_1_1",
],
]
];
echo "<table>";
echo "<tr><th>XMU_1_1 port</th><th>XMU_1_1 band</th></tr>";
foreach($array['XMU_1_1'] as $v) {
echo "<tr><td>" . $v['port'] . "</td><td>" . $v['band'] . "</td></tr>";
}
echo "</table>";
<强>输出强>
XMU_1_1 port XMU_1_1 band
1
2
3
4 700