语法
array_splice ( $input, $offset [,$length [,$replacement]] );
定义与使用
array_splice() 函数会从输入数组中移除以偏移量和长度指定的元素,并在提供替换数组的元素时将其替换,返回包含提取元素的数组。
参数
| 参数 | 描述 |
|---|---|
| input | (必需)指定了一个数组 |
| offset | 指定函数将从哪里开始移除元素。0 = 第一个元素。 |
| length | (可选)指定将移除多少元素,以及返回数组的长度。 |
| replacement | (可选)指定了一个数组,包含将要插入到原始数组中的元素。 |
返回值
返回数组的最后一个值,将数组缩短一个元素。
示例
试试以下示例:
<?php
$input = array("red", "black", "pink", "white");
array_splice($input, 2);
print_r($input);
print_r("<br />");
$input = array("red", "black", "pink", "white");
array_splice($input, 1, -1);
print_r($input);
print_r("<br />");
$input = array("red", "black", "pink", "white");
array_splice($input, 1, count($input), "orange");
print_r($input);
print_r("<br />");
$input = array("red", "black", "pink", "white");
array_splice($input, -1, 1, array("black", "maroon"));
print_r($input);
print_r("<br />");
$input = array("red", "black", "pink", "white");
array_splice($input, 3, 0, "purple");
print_r($input);
print_r("<br />");
?>
这将得到以下结果:
Array ( [0]=>red [1] =>black )
Array ( [0]=>red [1] =>white )
Array ( [0]=>red [1] =>orange )
Array ( [0]=>red [1] =>black [2]=>pink [3]=>black [4]=>maroon )
Array ( [0]=>red [1] =>black [2]=>pink [3]=>purple [4]=>white )
Array ( [0]=>red [1] =>white )
Array ( [0]=>red [1] =>orange )
Array ( [0]=>red [1] =>black [2]=>pink [3]=>black [4]=>maroon )
Array ( [0]=>red [1] =>black [2]=>pink [3]=>purple [4]=>white )

