array_splice():把数组中的一部分去掉并用其他值取代.
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(),除非该单元本身就是一个数组.例:
<?php
$input = array("red", "green", "blue", "yellow");
$back=array_splice($input, 2);
// $input 现在为 array("red", "green")
echo'back:';
print_r($back);
echo"<br>";
echo'input:';
print_r($input);
echo"<br>";
echo"<br>";
$input = array("red", "green", "blue", "yellow");
$back=array_splice($input, 1, -1);
// $input 现在为 array("red", "yellow")
echo'back:';
print_r($back);
echo"<br>";
echo'input:';
print_r($input);
echo"<br>";
echo"<br>";
$input = array("red", "green", "blue", "yellow");
$back=array_splice($input, 1, count($input), "orange");
// $input 现在为 array("red", "orange")
echo'back:';
print_r($back);
echo"<br>";
echo'input:';
print_r($input);
echo"<br>";
echo"<br>";
$input = array("red", "green", "blue", "yellow");
$back=array_splice($input, -1, 1, array("black", "maroon"));
// $input 现在为 array("red", "green",
// "blue", "black", "maroon")
echo'back:';
print_r($back);
echo"<br>";
echo'input:';
print_r($input);
echo"<br>";
echo"<br>";
$input = array("red", "green", "blue", "yellow");
$back=array_splice($input, 3, 0, "purple");
// $input 现在为 array("red", "green",
// "blue", "purple", "yellow");
echo'back:';
print_r($back);
echo"<br>";
echo'input:';
print_r($input);
?>
输出结果:
back:Array
(
[0] => blue
[1] => yellow
)
input:Array
(
[0] => red
[1] => green
)
back:Array
(
[0] => green
[1] => blue
)
input:Array
(
[0] => red
[1] => yellow
)
back:Array
(
[0] => green
[1] => blue
[2] => yellow
)
input:Array
(
[0] => red
[1] => orange
)
back:Array
(
[0] => yellow
)
input:Array
(
[0] => red
[1] => green
[2] => blue
[3] => black
[4] => maroon
)
back:Array
(
)
input:Array
(
[0] => red
[1] => green
[2] => blue
[3] => purple
[4] => yellow
)