|
array_slice (PHP 4, PHP 5) array_slice -- 從陣列中取出一段 說明array array_slice ( array array, int offset [, int length [, bool preserve_keys]] )
array_slice() 返回根據 offset 和
length 參數所特殊的 array
陣列中的一段序列。
若果 offset 非負,則序列將從 array
中的此偏移量開始。若果 offset 為負,則序列將從
array 中距離末端這麼遠的地方開始。
若果給出了 length 並且為正,則序列中將具有這麼多的單元。若果給出了
length 並且為負,則序列將終止在距離陣列末端這麼遠的地方。若果省略,則序列將從
offset 開始一直到 array 的末端。
注意 array_slice() 預設將重設陣列的鍵。自
PHP 5.0.2 起,可以通過將
preserve_keys 設為 TRUE 來改變此行為。
例子 1. array_slice() 例子
<?php $input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e" $output = array_slice($input, -2, 1); // returns "d" $output = array_slice($input, 0, 3); // returns "a", "b", and "c"
// note the differences in the array keys print_r(array_slice($input, 2, -1)); print_r(array_slice($input, 2, -1, true)); ?>
|
上例將輸出: Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
) |
|
參見 array_splice() 和 unset()。
| |