array_splice
(PHP4)
array_splice --- 移除陣列的一部份且替代它
語法 : array array_splice(array input, int offset, int [length] , array [replacement] );
說明 :
此函式將陣列input從offset到length移除,若有提供參數replacement則以replacement的元素來替代。
若offset為正數則移除的起始處從陣列array的開始處第offset的位置開始,若offset為負數則從陣列array的末端開始。
若省略參數length則移除的部份是從offset到陣列末端,若有指定length且是正數則移除length個元素,若有指定length且是負數則移除的部份將會止於陣列的末端第length個元素。
若有指定參數replacement則移除的元素將會以此陣列的元素來替代,若offset和length無移除,則replacement中的元素將會插入在offset所指定處。
以下的義意是相等的 :
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))
$a[$x] = $y array_splice($input, $x, 1, $y)
Example :
<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2); // $input is now array("red", "green")
array_splice($input, 1, -1); // $input is now array("red", "yellow")
array_splice($input, 1, count($input), "orange"); // $input is now array("red", "orange")
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green", "blue", "black", "maroon")
?>
參考 : array_slice