php一个三维数组foreach如何遍历

2024-12-15 17:46:37
推荐回答(2个)
回答1:

数组遍历就用foreach了。三维只是遍历三次而已。原理是一样。比如

$arr = array(array(array(1)));
foreach ($arr as $key => $value) {
    foreach ($value as $k => $v) {
        foreach ($v as $k3 => $v3) {
            var_dump($v3);
        }
    }
}
//最后输出的就是最里面的1了

回答2:

$array1 = array(
'files' => array(
'name' => array(
0 => 'a.gif',
1 => 'b.png',
2 => 'c.txt',
3 => 'd.css',
),
'tmp_name' => array(
0 => 'c:\windows\temp\php12.tmp',
1 => 'c:\windows\temp\php13.tmp',
2 => 'c:\windows\temp\php14.tmp',
3 => 'c:\windows\temp\php15.tmp',
),
'size' => array(
0 => 526632,
1 => 8222,
2 => 2001,
3 => 36 ,
),
'error' => array(
0 => 0,
1 => 0,
2 => 0,
3 => 0,
),
'type' => array(
0 => 'image/gif',
1 => 'image/png',
2 => 'text/plain',
3 => 'text/css',
),
)

);
$array2=array();
foreach($array1 as $key=>$value){
for($i=0;$i $array2[]=array(
'name'=>$value['name'][$i],
'type'=>$value['type'][$i],
'error'=>$value['error'][$i],
'tmp_name'=>$value['tmp_name'][$i],
'size'=>$value['size'][$i],
'extension'=>trim(strrchr($value['name'][$i],'.'),'.')
);
}
}
print_r($array2);