PHP array_splice() Function

PHP array_splice Function


The array_splice() function removes a portion or slice of an array and replaces it with the elements of another array. If no replacement array is specified, this function simply removes the elements.

Tip: If the function does not remove any elements (length=0), the replaced array will be inserted from the position of the start parameter.

Syntax

  array_splice(array, start, length, array)

Parameter :
Parameter Description
array Required. Specifies an array
start Required. Numeric value. Specifies where the function will start removing elements. 0 = the first element. If this value is set to a negative number, the function will start that far from the last element. -2 means start at the second last element of the array.
length Optional. Numeric value. Specifies how many elements will be removed, and also length of the returned array. If this value is set to a negative number, the function will stop that far from the last element. If this value is not set, the function will remove all elements, starting from the position set by the start-parameter.
array Optional. Specifies an array with the elements that will be inserted to the original array. If it's only one element, it can be a string, and does not have to be an array.

 Example : Remove elements from an array and replace it with new elements:

Output : 

Array
(
    [0] => promotion
    [1] => website
    [c] => House
    [d] => Online
)

Example : we can below same example as Example1,but output returend array

Output :

Array
(
    [a] => web
    [b] => designing
)