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


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. $array = array_reverse($array, True);
  2. $array["x"] = "z";
  3. $array = array_reverse($array, True);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.