php - complicate array push to the first index -
i have multidimensional array create like
$column = array( "person#1"=> array ("name"=>"leon" , "age"=>"19" ), "person#2"=> array ("name"=>"gary" , "age"=>"31" ), "person#3"=> array ("name"=>"may" , "age"=>"25" ) )
now, want push like
"person#4"=> array ("name"=>"tony" , "age"=>"28" )
to first place of array column.
i had into
array_unshift($column, array("person#1"=> array ("name"=>"tony" , "age"=>"28" ));
it did replace array , variable array, indexed '0' , not person#4
expect
use 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); ?>
shows:
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" } }
Comments
Post a Comment