array_splice()函數(shù)可以用來刪除數(shù)組中的某個(gè)片段,并用其他數(shù)組元素替換它。它的語法如下:
array_splice(array &$input, int $offset [, int $length = 0 [, mixed $replacement = [] ]]) : array
參數(shù)說明:
示例:
$colors = array('red', 'green', 'blue', 'yellow', 'purple');
array_splice($colors, 2, 0, array('black')); // 在第3個(gè)位置插入'black'
print_r($colors); // 輸出:Array ( [0] => red [1] => green [2] => black [3] => blue [4] => yellow [5] => purple )
array_splice($colors, 1, 2); // 刪除第2、3個(gè)元素
print_r($colors); // 輸出:Array ( [0] => red [1] => blue [2] => yellow [3] => purple )
注意:array_splice()函數(shù)會(huì)改變?cè)瓟?shù)組,建議在操作數(shù)組前先對(duì)數(shù)組進(jìn)行備份。