131

How can I add an item to the beginning of an associative array? For example, say I have an array like this:

$arr = array('key1' => 'value1', 'key2' => 'value2');

When I add something to it as in $arr['key0'] = 'value0';, I get:

Array
(
    [key1] => value1
    [key2] => value2
    [key0] => value0
)

How do I make that to be

Array
(
    [key0] => value0
    [key1] => value1
    [key2] => value2
)

Thanks,
Tee

outis
  • 75,655
  • 22
  • 151
  • 221
teepusink
  • 27,444
  • 37
  • 107
  • 147
  • Note that [minimal code samples](https://sscce.org/) are much more helpful than variable dumps alone (though the latter can also be helpful). – outis Apr 25 '11 at 22:04
  • possible duplicate of [PHP prepend associative array with literal keys?](http://stackoverflow.com/questions/1371016/php-prepend-associative-array-with-literal-keys). See also: [PHP: Array_unshift an not numeric index](http://stackoverflow.com/questions/4516664/php-array-unshift-an-not-numeric-index). – outis Apr 25 '11 at 22:11

5 Answers5

276

You could use the union operator:

$arr1 = array('key0' => 'value0') + $arr1;

or array_merge.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 4
    Which is more efficient? union or array_merge? If I may ask... – Melvin Jun 14 '13 at 06:34
  • 5
    @melvin: Perhaps you could do a performance test and tell us. – Vael Victus Aug 20 '13 at 20:49
  • 2
    I'm pretty sure union is faster. – Mathijs Segers Dec 31 '13 at 13:25
  • and `array_merge` didn't work with this: `$years = array_merge(array('2014'=>'2014'),$years);` for some reason, while the union operator did work. – Timo Huovinen Jan 14 '14 at 08:45
  • 5
    @Timo Huovinen, array_merge didn't work because PHP converted your key to a number, and array_merge resets numeric keys. – meustrus Jan 24 '14 at 18:16
  • @toby: `array_merge` does work with non-numeric keys, and the documentation actually explains what happens to numeric keys: *"Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array."* – Felix Kling Apr 03 '14 at 03:14
  • 4
    @FelixKling: How did I not know about this union operator? Amazing. This is great! – Wes Crow May 16 '14 at 12:47
  • I don't know why, but in a forloop overwriting the same array (in this answer `$arr1`) with this technique ended me up with an array with only one item. `array_merge` did work however. – gitaarik Jun 10 '15 at 23:54
  • +1 for this brilliant trick! Was able to sort a multidimensional array and then prepend the array. Worked like a charm! – cbloss793 Aug 23 '16 at 15:29
22

One way is with array_merge:

<?php
$arr = array('key1' => 'value1', 'key2' => 'value2');
$arr = array_merge(array('key0' => 'value0'), $arr);

Depending on circumstances, you may also make use of ksort.

outis
  • 75,655
  • 22
  • 151
  • 221
  • 3
    It doesn't matter in this instance, but if an element with key `'key0'` already existed in `$arr` then this _value_ would overwrite the new value being prepended (ie. `'value0'` would get overwritten). You can use the [union operator (`+`)](http://www.php.net/manual/en/language.operators.array.php) to resolve this. – MrWhite Mar 25 '13 at 12:17
7
$array = array('key1' => 'value1', 'key2' => 'value2');
array_combine(array_unshift(array_keys($array),'key0'),array_unshift(array_values($array),'value0'))
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
3
function unshift( array & $array, $key, $val)
{
    $array = array_reverse($array, 1);
    $array[$key] = $val;
    $array = array_reverse($array, 1);

    return $array;
}
Tomek
  • 49
  • 1
  • 3
2

If you don't want to merge the arrays you could just use ksort() on the array before iterating over it.

James C
  • 14,047
  • 1
  • 34
  • 43