offset | 如果 offset 為正,則從 input 數組中該值指定的偏移量開始移除。如果 offset 為負,則從 input 末尾倒數該值指定的偏移量開始移除。 |
length | 如果省略 length,則移除數組中從 offset 到結尾的所有部分。如果指定了 length 并且為正值,則移除這么多單元。如果指定了 length 并且為負值,則移除從 offset 到數組末尾倒數 length 為止中間所有的單元。 如果設置了 length 為零,不會移除單元。 小竅門:當給出了 replacement 時要移除從 offset 到數組末尾所有單元時,用 count($input) 作為 length。 |
replacement | 如果給出了 replacement 數組,則被移除的單元被此數組中的單元替代。 |
如果 offset 和 length 的組合結果是不會移除任何值,則 replacement 數組中的單元將被插入到 offset 指定的位置。 注意替換數組中的鍵名不保留。
如果用來替換 replacement 只有一個單元,那么不需要給它加上 array(),除非該單元本身就是一個數組、一個對象或者 NULL。
?php $input = array("red", "green", "blue", "yellow"); $x = "black"; $y = "purple"; // 添加兩個新元素到 $input array_push($input, $x, $y); array_splice($input, count($input), 0, array($x, $y)); // 移除 $input 中的最后一個元素 array_pop($input); array_splice($input, -1); // 移除 $input 中第一個元素 array_shift($input); array_splice($input, 0, 1); // 在 $input 的開頭插入一個元素 array_unshift($input, $x, $y); array_splice($input, 0, 0, array($x, $y)); // 在 $input 的索引 $x 處替換值 $input[$x] = $y; // 對于鍵名和偏移量等值的數組 array_splice($input, $x, 1, $y);
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php常用函數與技巧總結》、《PHP錯誤與異常處理方法總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。