-2

我尝试了很多无法从嵌套数组中获取值

这是数组:

$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

请帮忙

4

5 回答 5

2

您可以使用 foreach 循环:

$array; // this is your 2d array
foreach ($array as $k) {
    $out = "";
    foreach ($k as $n) {
        $out .= $n." ";
    }
    print_r($out."\n");
}
于 2013-07-16T14:35:43.733 回答
2
$arr = [
    [20, 21, 22],
    [20, 21, 22],
    [20, 21, 22],
    [20, 21, 22]
];

foreach ($arr as $row) {
  echo implode(' ', $row) . '<br />';
}
于 2013-07-16T14:36:29.973 回答
1

此代码将循环遍历数组的每个顶级元素,并为每个外部数组元素输出行元素。

<table>
<?php foreach ($array as $row): ?>
    <tr><td><?php echo implode("</td><td>", $row) ?></td></tr>
<?php endforeach; ?>
</table>
于 2013-07-16T14:34:58.747 回答
0
function recursion($arr){
   foreach($arr as $key => $val){
       if (is_array($val)){
           recursion($val);
       } else {
           print($val." ");
       }
   }
}
于 2013-07-16T14:35:18.717 回答
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";
}
于 2013-07-16T14:43:50.893 回答