我创建了一个多维数组就像
$column = array(
"person#1"=> array ("name"=>"Leon" , "Age"=>"19" ),
"person#2"=> array ("name"=>"Gary" , "Age"=>"31" ),
"person#3"=> array ("name"=>"May" , "Age"=>"25" )
)
现在,我想要推送一些像
这样的东西"person#4"=> array ("name"=>"Tony" , "Age"=>"28" )
移到数组列的第一个位置。
I had look into
array_unshift($column, array("person#1"=> array ("name"=>"Tony" , "Age"=>"28" ));
它确实用我的数组替换了数组和变量,但总是以'0'索引,而不是期望的person#4
为什么不做一个简单的$new_person + $column
呢?
使用array_merge()
:
<?php
header('Content-Type: text/plain');
$column = array(
"person#1"=> array ("name"=>"Leon" , "Age"=>"19" ),
"person#2"=> array ("name"=>"Gary" , "Age"=>"31" ),
"person#3"=> array ("name"=>"May" , "Age"=>"25" )
);
$column = array_merge(array( "person#4" => array ("name" => "Tony" , "Age" => "28" )), $column);
var_dump($column);
?>
节目:
array(4) {
["person#4"]=>
array(2) {
["name"]=>
string(4) "Tony"
["Age"]=>
string(2) "28"
}
["person#1"]=>
array(2) {
["name"]=>
string(4) "Leon"
["Age"]=>
string(2) "19"
}
["person#2"]=>
array(2) {
["name"]=>
string(4) "Gary"
["Age"]=>
string(2) "31"
}
["person#3"]=>
array(2) {
["name"]=>
string(3) "May"
["Age"]=>
string(2) "25"
}
}