Return to Snippet

Revision: 51725
at October 2, 2011 12:24 by lemcoe9


Initial Code
$array = array_reverse($array, True);
$array["x"] = "z";
$array = array_reverse($array, True);

Initial URL

                                

Initial Description
There seems to be no easy way to insert a key and value at the beginning of an array, as array_unset does not allow you to specify a key. This is not a very efficient way to do it, but it works. 

Pretend the array is currently: array("a"=>"b", "c"=>"d") and you would like "x"=>"z" at the beginning....

The snippet will result in $array being: array("x"=>"z", "a"=>"b", "c"=>"d")

Initial Title
Insert a Key=>Value at the beginning of an array

Initial Tags
php, sort, array

Initial Language
PHP