我必须用php迭代多维数组。我的数组数据如下:
Array
(
[id] => Array
(
['2'] => Array
(
[0] => 2
)
['1'] => Array
(
[0] => 1
)
)
[summary] => Array
(
['2'] => Array
(
[0] => Summary 1
)
['1'] => Array
(
[0] => Summary 2
)
)
[review] => Array
(
['2'] => Array
(
[0] => Review 2
)
['1'] => Array
(
[0] => Summary 2
)
)
[nickname] => ABCD
)
我想制作一组结果,例如array('id','summary','review', 'nickname')
答案 0 :(得分:1)
你可以试试这个:
$array = array(
'id' => array(2 => array(2), 1 => array(1)),
'summary' => array(2 => array('Summary ' . 1), 1 => array('Summary ' . 2)),
'review' => array(2 => array('Review ' . 2), 1 => array('Review ' . 2)),
'nickname' => 'ABCD'
);
$out = array();
foreach ($array as $col => $data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
$out[$key][$col] = $value[0];
}
}
}
foreach ($out as &$data) {
$data += array('nickname' => $array['nickname']);
}
var_dump($out);
输出:
array (size=2)
2 =>
array (size=4)
'id' => int 2
'summary' => string 'Summary 1' (length=9)
'review' => string 'Review 2' (length=9)
'nickname' => string 'ABCD' (length=4)
1 => &
array (size=4)
'id' => int 1
'summary' => string 'Summary 2' (length=9)
'review' => string 'Review 2' (length=9)
'nickname' => string 'ABCD' (length=4)