Program to Insert new item in array on any position in PHP (original) (raw)

Last Updated : 11 Jul, 2025

New item in an array can be inserted with the help of array_splice() function of PHP. This function removes a portion of an array and replaces it with something else. If offset and length are such that nothing is removed, then the elements from the replacement array are inserted in the place specified by the offset.

**Syntax:

array array_splice ($input, offset[,offset [, offset[,length [, $replacement]])

**Parameters:

This function takes four parameters out of which 2 are mandatory and 2 are optional:

**Return Value:

It returns the last value of the array, shortening the array by one element. **Note that keys in replacement array are not preserved. Program

php `

originalarray,original_array, originalarray,position, 0, $inserted_value ); echo "After inserting 11 in the array is : "; foreach ($original_array as $x) { echo "$x "; } ?>

`

Output

Original array : 1 2 3 4 5 After inserting 11 in the array is : 1 2 11 3 4 5

**Time Complexity: O(n)

**Auxiliary Space: O(1)

**Using Array Slicing and Concatenation

To insert a new item into an array at any position in PHP without using `array_splice()`, use `array_slice()` to split the array at the insertion point, then `array_merge()` to concatenate the parts with the new item inserted in between. This method maintains array integrity and flexibility.

**Example: This example shows the implementation of the above-mentioned approach.

PHP `

firstPart=arrayslice(firstPart = array_slice(firstPart=arrayslice(fruits, 0, insertIndex);<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>e</mi><mi>c</mi><mi>o</mi><mi>n</mi><mi>d</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>t</mi><mo>=</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><msub><mi>y</mi><mi>s</mi></msub><mi>l</mi><mi>i</mi><mi>c</mi><mi>e</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">secondPart=arrayslice(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">seco</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">d</span><spanclass="mordmathnormal"style="margin−right:0.13889em;">P</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">r</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">rr</span><spanclass="mordmathnormal">a</span><spanclass="mord"><spanclass="mordmathnormal"style="margin−right:0.03588em;">y</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:−2.55em;margin−left:−0.0359em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight">s</span></span></span></span><spanclass="vlist−s">​</span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal"style="margin−right:0.01968em;">l</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">ce</span><spanclass="mopen">(</span></span></span></span>fruits,insertIndex); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>e</mi><mi>c</mi><mi>o</mi><mi>n</mi><mi>d</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>t</mi><mo>=</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><msub><mi>y</mi><mi>s</mi></msub><mi>l</mi><mi>i</mi><mi>c</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">secondPart = array_slice(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">seco</span><span class="mord mathnormal">n</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">s</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">i</span><span class="mord mathnormal">ce</span><span class="mopen">(</span></span></span></span>fruits, insertIndex);<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>e</mi><mi>c</mi><mi>o</mi><mi>n</mi><mi>d</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>t</mi><mo>=</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><msub><mi>y</mi><mi>s</mi></msub><mi>l</mi><mi>i</mi><mi>c</mi><mi>e</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">secondPart=arrayslice(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">seco</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">d</span><spanclass="mordmathnormal"style="marginright:0.13889em;">P</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="marginright:0.02778em;">r</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="marginright:0.02778em;">rr</span><spanclass="mordmathnormal">a</span><spanclass="mord"><spanclass="mordmathnormal"style="marginright:0.03588em;">y</span><spanclass="msupsub"><spanclass="vlisttvlistt2"><spanclass="vlistr"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:2.55em;marginleft:0.0359em;marginright:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingresetsize6size3mtight"><spanclass="mordmathnormalmtight">s</span></span></span></span><spanclass="vlists"></span></span><spanclass="vlistr"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal"style="marginright:0.01968em;">l</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">ce</span><spanclass="mopen">(</span></span></span></span>fruits,insertIndex); // Insert new item in between the two parts fruits=arraymerge(fruits = array_merge(fruits=arraymerge(firstPart, [$newFruit], $secondPart); // Print the modified array print_r($fruits); ?>

`

Output

Array ( [0] => apple [1] => orange [2] => banana [3] => cherry )

Using Manual Array Manipulation

Manual array manipulation involves iterating through an array, conditionally inserting or modifying elements, and reconstructing the array. This approach provides flexibility but requires careful handling of array indices and potentially higher complexity compared to built-in functions like array_splice() or array_merge().

**Example

PHP `

key=>key => key=>value) { if ($key == 1) { $newArray[] = "orange"; } newArray[]=newArray[] = newArray[]=value; } array=array = array=newArray; print_r($array); ?>

`

Output

Array ( [0] => apple [1] => orange [2] => banana [3] => cherry )

**References : https://www.php.net/manual/en/function.array-splice.php