一聚教程网:一个值得你收藏的教程网站

热门教程

php array_splice定义和用法

时间:2022-06-24 20:05:05 编辑:袖梨 来源:一聚教程网

array_splice定义和用法
说明
array array_splice ( array &$input , int $offset [, int $length [, array $ replacement ]] )
array_splice() 把 input 数组中由 offset 和 length 指定的单元去掉,如果提供了 replacement 参数,则用 replacement 数组中的单元取代。返回一个包含有被移除单元的数组。注意 input 中的数字键名不被保留。

如果 offset 为正,则从 input 数组中该值指定的偏移量开始移除。如果 offset 为负,则从 input 末尾倒数该值指定的偏移量开始移除。

如果省略 length,则移除数组中从 offset 到结尾的所有部分。如果指定了 length 并且为正值,则移除这么多单元。如果指定了 length 并且为负值,则移除从 offset 到数组末尾倒数 length 为止中间所有的单元。小窍门:当给出了 replacement 时要移除从 offset 到数组末尾所有单元时,用 count($input) 作为 length。

如果给出了 replacement 数组,则被移除的单元被此数组中的单元替代。如果 offset 和 length 的组合结果是不会移除任何值,则 replacement 数组中的单元将被插入到 offset 指定的位置。注意替换数组中的键名不保留。如果用来替换的值只是一个单元,那么不需要给它加上 array(),除非该单元本身就是一个数组。

以下表达式以同样方式修改了 $input: array_splice() 等价表达式 array_push($input, $x, $y)  array_splice($input,

 代码如下 复制代码
count($input), 0, array($x, $y)) 
array_pop($input)  array_splice($input, -1) 
array_shift($input)  array_splice($input, 0, 1) 
array_unshift($input, $x, $y)  array_splice($input, 0, 0, array($x, $y)) 
$input[$x] = $y // 对于键名和偏移量等值的数组  array_splice($input, $x, 1, $y) 


返回一个包含被移除单元的数组。

 

例子 1

 代码如下 复制代码
php
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
$a2=array(0=>"Tiger",1=>"Lion");
array_splice($a1,0,2,$a2);
print_r($a1);
?>

输出:

Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird )例子 2
与例子 1 相同,但是输出返回的数组:

 代码如下 复制代码
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
$a2=array(0=>"Tiger",1=>"Lion");
print_r(array_splice($a1,0,2,$a2));
?>

输出:

Array ( [0] => Dog [1] => Cat )例子 3
length 参数设置为 0:

 代码如下 复制代码
$a1=array(0=>"Dog",1=>"Cat");
$a2=array(0=>"Tiger",1=>"Lion");
array_splice($a1,1,0,$a2);
print_r($a1);
?>

输出:

Array ( [0] => Dog [1] => Tiger [2] => Lion [3] => Cat )


应用实例

 代码如下 复制代码

$input1 = array("red", "green", "blue", "yellow");
$input2 = array_splice($input1, 2);

//打印剩下的
print_r($input1);

//打印被移走的
print_r($input2);

因为这个函数的第一个参数是地址引用,返回值是被移走的部份。如果你只是想看剩下的。这样写 就可以了。

 代码如下 复制代码
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
print_r($input);

===================================================
我把手册的例子加了一个说明。。
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
//从第2个之后开始选,到剩下的全部,选中的移走。
//也就是 "blue", "yellow" 被选中
// $input is now array("red", "green")

array_splice($input, 1, -1);
//从第1个之后开始选,到剩下的全部倒回来一个,选中的移走。
//也就是 "green", "blue",被选中
// $input is now array("red", "yellow")

array_splice($input, 1, count($input), "orange");
//从第1个之后开始选,到剩下的全部,选中的移走,在当前指针位置加一个新值。
//也就是 "green", "blue", "yellow" 被选中
// $input is now array("red", "orange")

array_splice($input, -1, 1, array("black", "maroon"));
//从最后1个之前开始选,往下选1个,选中的移走,在当前指针位置加进一个数组。
//也就是 "yellow" 被选中
// $input is now array("red", "green","blue", "black", "maroon")

array_splice($input, 3, 0, "purple");
//从第3个之后开始选,一个都不选,在当前指针位置插入新值。
//位置就在 "red", "green", "blue" 和 "yellow" 之间
// $input is now array("red", "green", "blue", "purple", "yellow");

热门栏目