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($array ,2 ,3)此段代碼表示從$array數組中從第二個開始,取三個值;
array_slice($array ,3)此時代碼表示從$array數組中第三個之后的所有數組。
array_slice() 例子
[php] view plain copy <?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)); ?>
<?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 ( [0] => c [1] => d ) Array ( [2] => c [3] => d )