Insert a New Element After a Given Element in an Array in PHP

Given an array containing some elements, we want to insert a new item immediately after a specified element in the array. To insert a new element after a given element in an array, we need to locate the position of the given element and then insert the new element at the correct position.

Below are the approaches to insert a new element after a given element in an array in PHP:

Table of Content

  • Using array_splice() Function
  • Using foreach Loop
  • Using array_slice() Function

Using array_splice() Function

The array_splice() function can be used to insert an element at a specific position in an array. This method involves finding the index of the given element and then inserting the new element at the next index.

Example: The array_search() function finds the index of 30, and array_splice() inserts 35 at the position after 30.

PHP
<?php
function insertElement($arr, $element,
                       $newElement) {    
    $position = array_search($element, $arr);
    if ($position !== false) {
        $position++;
        array_splice($arr, $position,
                     0, $newElement);
    }   
    return $arr;
}

$arr = [10, 20, 30, 40, 50];
$newArr = insertElement($arr, 30, 35);
print_r($newArr);
?>

Output
Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 35
    [4] => 40
    [5] => 50
)

Using foreach Loop

A foreach loop can be used to iterate through the array, and a new element can be added to a new array after the given element is found.

Example: The foreach loop iterates through the array, and when 30 is encountered, then 35 is added to the new array immediately after 30.

PHP
<?php
function insertElement($arr, $element,
                       $newElement) {
    $newArr = [];
    foreach ($arr as $value) {
        $newArr[] = $value;
        if ($value === $element) {
            $newArr[] = $newElement;
        }
    }
    return $newArr;
}
$arr = [10, 20, 30, 40, 50];
$newArr = insertElement($arr, 30, 35);
print_r($newArr);
?>

Output
Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 35
    [4] => 40
    [5] => 50
)

Using array_slice() Function

The array_slice() function can be used to split the array into two parts before and after the given element. The new element can then be inserted between these two parts.

Example: The array_slice() function splits the array at the position after 30, and array_merge() combines the two parts with 35 in between.

PHP
<?php
function insertElement($arr, $element,
                       $newElement) { 
    $pos = array_search($element, $arr);
    
    if ($pos !== false) {
        $before = array_slice($arr, 0, $pos + 1);
        $after = array_slice($arr, $pos + 1);
        return array_merge($before,
                           [$newElement], $after);
    }
    
    return $array;
}
$arr = [10, 20, 30, 40, 50];
$newArr = insertElement($arr, 30, 35);

print_r($newArr);

?>

Output
Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 35
    [4] => 40
    [5] => 50
)