English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Online Tools

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

Object Functions

PHP Array Function Manual

PHP array_splice() Function Usage and Example

The PHP array_splice() function removes a part of an array and replaces it with other values

Syntax

array_splice ( $input, $offset [,$length [,$replacement]] );

Definition and UsageThis function frominputIt specifies an arraydelete the elements in the array byandlengthThe specified element, andreplacement

Replace the elements of the array (if provided) with them. It returns an array containing the extracted elements.

Note that the numeric keys in input are not retained.

ParameterSerial Number
1

Parameters and Description

input(required)

2

It specifies an array

offset

3

It specifies where the function will start deleting elements. 0 = the first element.

length(optional)

4

It specifies the number of elements to be deleted and the length of the returned array.

replacement(optional)

It specifies an array that contains the elements to be inserted into the original array.

 Return Value

Returns an array containing the removed elements.

Online Example

Example
   , array("black", "maroon"));
   $input = array("red", "black", "pink", "white"); 2<?php
   , 0, "purple");
   print_r($input); /print_r("<br
   , array("black", "maroon"));
   $input = array("red", "black", "pink", "white"); 1, count($input), "orange"); -1<?php
   , 0, "purple");
   print_r($input); /print_r("<br
   , array("black", "maroon"));
   $input = array("red", "black", "pink", "white"); 1);
   , 0, "purple");
   print_r($input); /print_r("<br
   , array("black", "maroon"));
   $input = array("red", "black", "pink", "white"); -1, count($input), "orange"); 1,
   , 0, "purple");
   print_r($input); /print_r("<br
   , array("black", "maroon"));
   $input = array("red", "black", "pink", "white"); 3array_splice($input,
   , 0, "purple");
   print_r($input); /print_r("<br
});
?>/Test and see‹

Array ( [0]=>red [1Output Result:
Array ( [0]=>red [1] =>black )
Array ( [0]=>red [1] =>white )
Array ( [0]=>red [1] =>black [2pink [3] =>orange )4maroon )
Array ( [0]=>red [1] =>black [2pink [3purple [4white )

PHP Array Function Manual