array_slice()
函數從陣列中取出一段。首先來看一看語法:array_slice ( array $array , int $offset , int $length = null , bool $preserve_keys = false ) : array
$array:輸入的陣列。
$offset:規定取出元素的開始位置
$length:可選,規定被返回陣列的長度。
$preserve_keys:可選,規定函數是保留鍵名還是重置鍵名
返回值:返回其中一段。 如果 $offset 引數大於 $array 尺寸,就會返回空的 $array。
程式碼範例:
1.有兩個引數
<?php $a=array("red","green","blue","yellow","brown"); print_r(array_slice($a,1)); ?>
輸出:Array ( [0] => green[1] => blue[2] => yellow[3] => brown)
2.三個引數
<?php $a=array("red","green","blue","yellow","brown"); print_r(array_slice($a,1,3)); ?>
輸出:Array ( [0] => green[1] => blue[2] => yellow)
3.四個引數
<?php $a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"brown"); print_r(array_slice($a,1,2)); $a=array("0"=>"red","1"=>"green","2"=>"blue","3"=>"yellow","4"=>"brown"); print_r(array_slice($a,1,2)); ?>
輸出:Array([b] => green [c] => blue) Array([0] => green[1] => blue)
推薦:《》《》
以上就是array_slice()函數在PHP中的正確使用的詳細內容,更多請關注TW511.COM其它相關文章!