PHP Program to insert element into an array at specified index


January 9, 2023, Learn eTutorial
2149

What is an array ?

Arrays are one of the data structures in PHP. An array is used to store the same type of data in a contiguous memory location. We can store any amount of data in the array. The “array ()” function is used to create an array in PHP.

 

How to insert an element into the array at a certain position using PHP?

To insert an element into an array we need an index or position where it is to be placed because elements are fetched using array index only. To add an element to the end of an array is easy but we need shifting of elements to insert an element in between of array. PHP provides an in-built function  array_splice() to insert elements, let's make use of it in our program.

In this program, we are going to insert an element at a specified index. For that, we first declare and assign values into the array arr[]. After that, we need a new value that is going to insert into the array and assign a value to the variable newValue. Then we have to specify the position where we have to insert the new element and assign that value to the variable pos. And to insert the element we are using the built-in function array_splice(). In the function, we specify the array arr[], the position of the element in variable pos, and the newValue. And at last, we can print the elements of the array arr[] by using foreach loop.  

Syntax of array_splice() function


array_splice(src_array, start_index, length, replace_array)
 

The array_splice() function removes selected elements of size length from start_index of an array src_array and replaces them with new elements given in replace_array . The function also returns an array with the removed elements. 

NOTE: If the length= 0 then the function does not remove any array elements instead the replace_array element will be inserted from  the start_index position.


It is a simple array program in PHP using an array, loop, and in-built function. Let's go through these topics to understand this program clearly"

ALGORITHM

Step 1: Initialize an array arr[] with values

Step 2: Print the element currently in the array arr[] using foreach loop

Step 3: Assign the new value to be inserted to the variable newValue

Step 4: Assign the position of the element to be inserted into the variable pos

Step 5: Call the built-in function array_splice(arr, pos,0,newValue)

Step 6: Print the elements in the array arr[] using foreach loop

PHP Source Code

                                          <&questphp;
$arr = array(1, 2, 3, 4, 5);
echo "Array before inserting new element: \n";
foreach ($arr as $x) {
    echo "$x ";
}
$newValue = 23;
$pos = 2;
array_splice($arr, $pos, 0, $newValue);
echo "\nArray after inserting new element: \n";
foreach ($arr as $x) {
    echo "$x ";
}
?>
                                      

OUTPUT

Array before inserting new element:
1 2 3 4 5
Array after inserting new element:
1 2 23 3 4 5