How to insert an item at the beginning of an array in PHP?

How to insert an item at the beginning of an array in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Prepending in PHP: Performance and Semantics

Appending an item to the end of an array is a trivial operation in PHP, usually accomplished with $arr[] = $item;. This syntax relies on the engine automatically incrementing the internal index counter, which is optimized for sequential writes. However, inserting an item at the beginning requires explicit intervention because it disrupts the existing sequence of indices.

To prepend a value, you must manually shift all current elements down by one index and then assign the new value to the first position. The most direct approach using native PHP functions involves array_unshift. This function accepts the array as its first argument followed by any number of values to insert at the start.

$arr = [1, 2, 3];
array_unshift($arr, 'new_item');

// Result: ['new_item' => 0, 1 => 1, 2 => 2, 3 => 3]
// Note: The internal indices are renumbered starting from 0.

While array_unshift is concise, understanding its internal mechanics is crucial for performance-critical applications. When you prepend an item to a large array, PHP must iterate through the entire existing structure to update the key-value pairs of every subsequent element. This results in a time complexity of O(n). If you are performing this operation inside a loop, the total cost becomes quadratic, O(n²), which can lead to significant slowdowns on datasets exceeding thousands of elements.

If your application relies heavily on array indexing and expects sequential integer keys (like 0, 1, 2), array_unshift is the correct semantic choice. It maintains the invariant that the first key is 0. However, if you are working with sparse arrays or associative arrays where specific string keys hold meaning, shifting those indices might inadvertently break your data structure's integrity.

In scenarios where performance is paramount and you need to maintain a fixed index for specific elements without renumbering them, consider using a sentinel value strategy or switching to a dedicated collection class. For instance, if the first element represents a header that should always be at index 0 but other elements have non-sequential indices, array_unshift will still renumber everything else to fill the gap. This behavior can lead to bugs if your code assumes specific keys exist after insertion.

In the Laravel ecosystem, while native PHP functions like array_unshift are perfectly valid and widely used, the framework often encourages using collection methods for readability when dealing with Eloquent models or complex data transformations. However, for raw performance optimization in high-frequency loops, sticking to C-level extensions like array_unshift usually outperforms higher-level abstractions.

If you find yourself needing to manipulate arrays at the beginning of a list frequently, evaluate whether your data structure is appropriate. A linked list implementation would theoretically offer O(1) insertion at the head, but in PHP, implementing such structures adds overhead compared to native arrays unless you are using specific libraries like SplDoublyLinkedList. Remember that SplDoublyLinkedList does not support direct numeric indexing in the same way standard arrays do, so switching to it requires a refactor of how your data is accessed.

Ultimately, the choice depends on the size of your dataset and the constraints of your existing codebase. For small datasets, readability should take precedence. For large-scale processing, benchmarking array_unshift against alternative strategies in your specific environment is essential before making architectural changes.

Our network