The Wayback Machine - https://web.archive.org/web/20240619183418/https://www.geeksforgeeks.org/program-to-insert-new-item-in-array-on-any-position-in-php/
Open In App

Program to Insert new item in array on any position in PHP

Last Updated : 02 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




<?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 ";
}
?>


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)

References : http://php.net/manual/en/function.array-splice.php


Previous Article
Next Article

Similar Reads

How to insert an item at the beginning of an array in PHP ?
Arrays in PHP are a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data. The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key. There are two methods to
3 min read
How to Insert a New Element in an Array in PHP ?
In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key. We can insert an element or item in an array using the below functions: Table of Content Using array_unshift() Functio
5 min read
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 giv
3 min read
How to insert an item into array at specific index in JavaScript ?
To insert an item into the array at a specific index we will break the array from the index and insert the item into the array. There is no inbuilt method in JavaScript that directly allows for the insertion of an element at any arbitrary index of an array. The possible approaches are given below. Let's create a JavaScript program having an array,
4 min read
Insert string at specified position in PHP
Given a sentence, a string and the position, the task is to insert the given string at the specified position. We will start counting the position form zero. See the examples below. Input : sentence = 'I am happy today.' string = 'very' position = 4 Output :I amvery happy today. Begin counting with 0. Start counting from the very first character ti
1 min read
How to Insert Characters in a String at a Certain Position in PHP ?
In this article, you will learn how to insert characters into a string at a specific position in PHP. Given a string str and an integer position that describes the index in the original string where the desired string insertStr will be added. This can be achieved using various approaches, such as string concatenation, and the substr_replace() funct
2 min read
New self vs. new static in PHP
New self: The self is keyword in PHP. It refers to the same class in which the new keyword is actually written. It refers to the class members, but not for any particular object. This is because the static members(variables or functions) are class members shared by all the objects of the class. The function called self::theFunction() behaves like "
4 min read
PHP program to add item at the beginning of associative array
In PHP associative array is the type of array where the index need not to be strictly sequential like indexed array. Normally add a new element in an existing associative array it will get appended at the end of that array. Example: &lt;?php // Existing array $arr = array('one' =&gt; 1, 'two' =&gt; 2); // New element $arr['zero'] = 0; // Final arra
3 min read
How to insert text into the textarea at the current cursor position?
To insert text into the textarea/inputbox at the current cursor position we need to get the current position of the cursor. So here the approach to get the current position of the cursor. First, get the current position of cursor with the help of property named as selectionStart on textarea/inputbox.To insert the text at the given position we will
2 min read
JavaScript Insert a string at position X of another string
Given two strings, the task is to insert one string in another at a specified position using JavaScript. We're going to discuss a few methods, these are: Methods to Insert String at a Certain IndexTable of Content Method 1: Using JavaScript String slice() and Array join() MethodsMethod 2: JavaScript String substr() Method Method 3: Using JavaScript
4 min read
Article Tags :