我已经尝试过很多无法从嵌套数组中获取值
这是数组:
$array = Array (
Array (20, 21, 22),
Array (20, 21, 23),
Array (20, 22, 23),
Array (21, 22, 23)
);
输出应该看起来像
20 21 22
20 21 23
20 22 23
21 22 23
请帮助
答案 0 :(得分:2)
您可以使用foreach循环:
$array; // this is your 2d array
foreach ($array as $k) {
$out = "";
foreach ($k as $n) {
$out .= $n." ";
}
print_r($out."\n");
}
答案 1 :(得分:2)
$arr = [
[20, 21, 22],
[20, 21, 22],
[20, 21, 22],
[20, 21, 22]
];
foreach ($arr as $row) {
echo implode(' ', $row) . '<br />';
}
答案 2 :(得分:1)
此代码将循环遍历数组的每个顶级元素,并为每个外部数组元素输出行元素。
<table>
<?php foreach ($array as $row): ?>
<tr><td><?php echo implode("</td><td>", $row) ?></td></tr>
<?php endforeach; ?>
</table>
答案 3 :(得分:0)
function recursion($arr){
foreach($arr as $key => $val){
if (is_array($val)){
recursion($val);
} else {
print($val." ");
}
}
}
答案 4 :(得分:0)
最简单的方法是使用foreach
遍历外部数组并内嵌数组:
$array = Array (
Array (20, 21, 22),
Array (20, 21, 23),
Array (20, 22, 23),
Array (21, 22, 23)
);
foreach ($array as $row) {
echo implode(" ", $row) . "\n";
}