Javatpoint Logo
Javatpoint Logo

PHP array_splice() function

The array_splice() function is an extended version of array_slice() function. The array_splice() function removes selected elements from an array and replaces it with new elements. This function was introduced in PHP 4.

Syntax

Parameters

Parameter Description Is compulsory
array Specifies an array. compulsory
Starting It Specifies where the function will start removing elements. compulsory
Length It specifies how many elements will be removed, and also length of the returned array. Optional
replacement If replacement array is specified, then the removed elements are replaced with elements from this array. Optional

Return Values

The array_splice( ) function returns an array which consists of the extracted elements.

Important note:

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. The keys in the replaced array are not preserved.

Example 1

Output:

Array
(
    [0] =>AbhinavBindra
    [1] =>Sushilkumar
)

Example 2

Output:

Array
(
    [0] =>AbhinavBindra
    [1] => Mary kom
)

Example 3

Output:

Array
(
    [0] =>AbhinavBindra
    [1] =>Vijendersingh
)

Example 4

Output:

Array
(
    [a] => apple
    [b] => banana
)





Help Others, Please Share

facebook twitter pinterest