array_splice — 把数组中的一部分去掉并用其它值取代
array_splice() 函数与 array_slice() 函数类似,选择数组中的一系列元素,但不返回,而是删除它们并用其它值代替。
说明
array array_splice ( array &input, int offset [, int length [, array replacement]] )
参数 | 描述 |
---|---|
提示:如果函数没有删除任何元素 (length=0),则替代数组将从 start 参数的位置插入。
注释:不保留替代数组中的键。
以下表达式以同样方式修改了 $input:
表 1. array_splice() 等价表达式
array_push($input, $x, $y) array_splice($input, count($input), 0, array($x, $y)) //array_push函数array_pop($input) array_splice($input, -1)//array_pop函数array_shift($input) array_splice($input, 0, 1) //array_shift函数array_unshift($input, $x, $y) array_splice($input, 0, 0, array($x, $y)) $input[$x] = $y // 对于键名和偏移量等值的数组 array_splice($input, $x, 1, $y)返回一个包含被移除单元的数组。
<?php$input = array("red", "green", "blue", "yellow");array_splice($input, 2);// $input is now array("red", "green")$input = array("red", "green", "blue", "yellow");array_splice($input, 1, -1);// $input is now array("red", "yellow")$input = array("red", "green", "blue", "yellow");array_splice($input, 1, count($input), "orange");// $input is now array("red", "orange")$input = array("red", "green", "blue", "yellow");array_splice($input, -1, 1, array("black", "maroon"));// $input is now array("red", "green",// "blue", "black", "maroon")$input = array("red", "green", "blue", "yellow");array_splice($input, 3, 0, "purple");// $input is now array("red", "green",// "blue", "purple", "yellow");?>
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
新闻热点
疑难解答