函数array_splice():选择数组中的一系列元素,但不返回,而是删除它们并用其它值代替。
<?php
$input = array( "Linux", "Apache", "MySQL", "PHP" );
//原数组中的第二个元素后到数组结尾都被删除
array_splice($input, 2);
print_r($input); //输出:Array ( [0] => Linux [1] => Apache )
$input = array("Linux", "Apache", "MySQL", "PHP");
//从第二个开始移除直到数组末尾倒数第1个为止中间所有的元素
array_splice($input, 1, -1);
print_r($input); //输出:Array ( [0] => Linux [1] => PHP )
$input = array( "Linux", "Apache", "MySQL", "PHP" );
//从第二个元素到数组结尾都被第4个参数替代
array_splice($input, 1, count($input), "web");
print_r($input); //输出:Array ( [0] => Linux [1] => web )
$input = array( "Linux", "Apache", "MySQL", "PHP" );
//最后一个元素被第4个参数数组替代
array_splice($input, -1, 1, array("web", "www"));
print_r($input); //输出:Array ( [0] => Linux [1] => Apache [2] => MySQL [3] => web [4] => www )
|