我有一个变量,例如:$Foods
,它存储如下数组:
array(
'Fruit' => 'Banana',
'cake' => array(
(int) 0 => '10',
(int) 1 => '11',
(int) 2 => '12'
)
)
我想要这样:
array(
'Fruit' => 'Banana',
'cake' => '10'
)
array(
'Fruit' => 'Banana',
'cake' => '11'
)
array(
'Fruit' => 'Banana',
'cake' => '12'
)
我怎样才能做到这一点?
答案 0 :(得分:2)
试试这段代码
<?php
$Foods = array('Fruit' => 'Banana', 'cake' => array('10','11','12'));
$newFoods = array();
foreach($Foods['cake'] as $key => $val):
$newFoods[$key]['Fruit'] = $Foods['Fruit'];
$newFoods[$key]['cake'] = $val;
endforeach;
print_r($newFoods);
?>
输出
Array
(
[0] => Array
(
[Fruit] => Banana
[cake] => 10
)
[1] => Array
(
[Fruit] => Banana
[cake] => 11
)
[2] => Array
(
[Fruit] => Banana
[cake] => 12
)
)