我有一个关联数组,我想添加更多的键和值
Array
(
[0] => Array
(
[NUMBER] => 67
[TYPE] => Other
[DATE] => 3/31/2011
)
[1] => Array
(
[NUMBER] => 87
[TYPE] => something
[DATE] => 3/28/2011
)
[2] => Array
(
[NUMBER] => 67
[TYPE] => Other
[DATE] => 3/2/2011
)
)
在上面的数组中,我想在DATE之前添加另一个名为STATUS的键和值 所以最后iget
Array
(
[0] => Array
(
[NUMBER] => 67
[TYPE] => Other
[STATUS] => waiting
[DATE] => 3/31/2011
)
}
canPlease给我正确的方向
答案 0 :(得分:2)
$arr = Array(
0 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/32/2011'),
1 => Array('NUMBER' => 87, 'TYPE' => something, 'DATE' => '3/28/2011'),
2 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/2/2011')
);
foreach($arr as $key => $value) {
$arr[$key] = array_slice($value, 0, 2) +
array('Status' => 'waiting') +
array_slice($value, -1);
}
var_dump($arr);
给出以下数组:
array(3) {
[0]=>
array(4) {
["NUMBER"]=>
int(67)
["TYPE"]=>
string(5) "Other"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(9) "3/32/2011"
}
[1]=>
array(4) {
["NUMBER"]=>
int(87)
["TYPE"]=>
string(9) "something"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(9) "3/28/2011"
}
[2]=>
array(4) {
["NUMBER"]=>
int(67)
["TYPE"]=>
string(5) "Other"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(8) "3/2/2011"
}
}