Posted By

lemcoe9 on 10/02/11


php sort beginning array key arrays Value reverse

Versions (?)

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

 / Published in: PHP

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")

  1. $array = array_reverse($array, True);
  2. $array["x"] = "z";
  3. $array = array_reverse($array, True);

Report this snippet  

You need to login to post a comment.