How to insert element into arrays at specific position?

How to insert element into arrays at specific position?

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

Stefan Bogdanescu

Founder & Senior Architect

Inserting Elements into Arrays at Specific Positions

When you need to modify a data structure by placing a new element between existing ones, the standard PHP array_push function is insufficient. Pushing an item onto an array always appends it to the end, shifting the original indices of all subsequent elements. If your application logic depends on maintaining a specific order—such as inserting a "sample" entry after the third item in a list—you must manipulate the internal index directly.

To achieve this cleanly without creating unnecessary intermediate arrays or re-indexing the entire dataset prematurely, you can use the array_splice function. This built-in utility allows you to remove zero elements from a specific offset and insert new data in their place. The syntax requires three primary arguments: the offset where insertion begins, the number of elements to remove (which is 0 in this case), and the array containing the new element(s).

Consider your starting arrays:

$array_1 = array(
    '0' => 'zero',
    '1' => 'one',
    '2' => 'two',
    '3' => 'three',
);

$array_2 = array(
    'zero'  => '0',
    'one'   => '1',
    'two'   => '2',
    'three' => '3',
);

Your goal is to insert ['sample_key' => 'sample_value'] immediately after the third element. In zero-based indexing, the third element resides at index 2. Therefore, the insertion offset must be set to 3. If you were using a foreach loop or iterating with a counter, you would increment that counter by one before passing it to array_splice to ensure the new item lands in the correct slot without overwriting existing data.

Here is how you implement this for both arrays:

$newElement = ['sample_key' => 'sample_value'];

// Insert after index 2 (the third element) into array_1
$array_1 = array_splice($array_1, 3, 0, $newElement);

// Insert after index 2 (the third element) into array_2
$array_2 = array_splice($array_2, 3, 0, $newElement);

After execution, both arrays will contain the new key-value pair at index 3. The original elements at indices 0, 1, and 2 remain intact, and the element originally at index 3 ('three') is shifted to index 4. This behavior preserves the integrity of your data while allowing for dynamic insertion points.

A critical pitfall in this operation involves handling associative arrays like $array_2. While array_splice works seamlessly with integer-based indices, you must ensure that the offset argument references a valid numeric position. If you attempt to splice based on a string key directly without converting it to an index first, PHP will throw a warning or fail to insert at the expected location. In the example above, even though $array_2 uses string keys ('zero', 'one', etc.), we are targeting a specific position relative to the start of the array (the 3rd item), not a specific key name.

Performance is also a factor to consider when scaling this logic. array_splice effectively unshifts or shifts elements in memory. While this is efficient for small-to-medium datasets, repeatedly inserting elements into the middle of large arrays in a loop can degrade performance due to continuous memory reallocation and shifting. If you are building a massive list element by element, it is often more performant to construct the final array first and then perform batch splicing operations or use iterator-based approaches if available in your framework context.

For Laravel developers, this pattern frequently appears when processing paginated results or queue jobs where metadata needs to be injected at specific points in a log or history record. While libraries like https://laravelcompany.com often discuss advanced data manipulation patterns, understanding the core array_splice mechanism ensures you can handle these scenarios without relying solely on framework helpers that might obfuscate the underlying array behavior.

Our network