Program to Insert new item in array on any position in PHP
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 [, $length [, $replacement]])
Parameters: This function takes four parameters out of which 2 are mandatory and 2 are optional:
- $input: This parameter takes the value of an array on which operations are needed to perform.
- $offset: If this parameter is positive then the start of removed portion is at that position from the beginning of the input array and if this parameter is negative then it starts that far from the end of the input array.
- $length: (optional) If this parameter is omitted then it removes everything from offset to the end of the array.
- If length is specified and is positive, then that many elements will be removed.
- If length is specified and is negative then the end of the removed portion will be that many elements from the end of the array.
- If length is specified and is zero, no elements will be removed.
- $replacement: (optional) This parameter is an optional parameter which takes value as an array and if this replacement array is specified, then the removed elements are replaced with elements from this replacement array.
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 //Original Array on which operations is to be perform $original_array = array( '1', '2', '3', '4', '5' ); echo 'Original array : '; foreach ($original_array as $x) { echo "$x "; } echo "\n"; //value of new item $inserted_value = '11'; //value of position at which insertion is to be done $position = 2; //array_splice() function array_splice( $original_array, $position, 0, $inserted_value ); echo "After inserting 11 in the array is : "; foreach ($original_array as $x) { echo "$x "; } ?> |
Original array : 1 2 3 4 5 After inserting 11 in the array is : 1 2 11 3 4 5
References : http://php.net/manual/en/function.array-splice.php
Recommended Posts:
- How to insert an item at the beginning of an array in PHP ?
- How to insert an item into array at specific index in JavaScript?
- PHP program to add item at the beginning of associative array
- Insert string at specified position in PHP
- Get the first and last item in an array using JavaScript
- How to replace an item from an array in JavaScript?
- HTML | DOM item() Method
- How to remove indentation from an unordered list item using CSS?
- How to make the cursor to hand when a user hovers over a list item using CSS?
- unordered_map insert in C++ STL
- list insert() in C++ STL
- unordered_multimap insert() in C++ STL
- unordered_multiset insert() function in C++ STL
- unordered_set insert() function in C++ STL
- PHP | Ds\Sequence insert() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



