Insert items at beginning of an array in PHP
This post will discuss how to insert items at the beginning of an array in PHP.
1. Using array_unshift() function
A simple and efficient solution is to use the array_unshift() function, which prepends one or more elements to the beginning of an array while maintaining the order of the prepended elements.
Following is a simple example demonstrating usage of this function for numerical key array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $arr = ['a', 'b', 'c']; $item = 'd'; array_unshift($arr , $item); print_r($arr); /* Output: Array ( [0] => d [1] => a [2] => b [3] => c ) */ ?> |
Note that the numerical keys in the array are re-indexed starting with zero. However, the literal keys won’t be changed, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $arr = ['b' => 'two', 'c' => 'three']; $item = 'one'; array_unshift($arr, $item); print_r($arr); /* Output: Array ( [0] => one [b] => two [c] => three ) */ ?> |
2. Using array_splice() function
Alternatively, you can use the array_splice() function, which replaces a part of an array with the elements of the specified array.
Here’s an example of its usage to insert an element at the specified position in an array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $arr = ['b', 'c']; $pos = 0; $item = 'a'; array_splice($arr, $pos, 0, $item); print_r($arr); /* Output: Array ( [0] => a [1] => b [2] => c ) */ ?> |
Note that, like array_unshift() function, array_splice() does not preserve numeric keys.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $arr = ['one', 'b' => 'two', 'c' => 'three']; $pos = 0; $item = 'zero'; array_splice($arr, $pos, 0, $item); print_r($arr); /* Output: Array ( [0] => zero [1] => one [b] => two [c] => three ) */ ?> |
3. Using + operator
In some cases, the above functions do not work since they always re-index the array. If you need to prepend an item to an array without re-indexing the keys or need to prepend a key-value pair to an associative array, you can + operator:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $arr = ['b' => 'two', 'c' => 'three']; $item = array('a' => 'one'); $arr = $item + $arr; print_r($arr); /* Output: Array ( [a] => one [b] => two [c] => three ) */ ?> |
This however creates a new array and doesn’t modify the array in-place.
4. Using array_reverse() function
Another plausible way to insert an item at the beginning of an associative array is to reverse the array, insert a key and value pair using the $arr[$key] = $val syntax, and then reverse it back to get the desired order.
This approach is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $arr = ['b' => 'two', 'c' => 'three']; $arr = array_reverse($arr); $arr['a'] = 'one'; $arr = array_reverse($arr); print_r($arr); /* Output: Array ( [a] => one [b] => two [c] => three ) */ ?> |
Like the + operator, this doesn’t modify the array in-place and creates a new array instead.
That’s all about inserting items at the beginning of an array in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)