PHP: Insert an element with the specified key at the beginning of the array
Issue
With array_unshift(), we can prepend one or more elements to the beginning of an array. All numerical array keys will be modified to start counting from zero while literal keys won't be changed.
Output:
It is not possible to add an element with the specified key at the front of the array using array_unshift()
or array_marge()
.
Solution
The +
operator won't reindex the keys in array. For example.
Output: