时间:2021-07-01 10:21:17 帮助过:8人阅读
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)
推荐:《2021年PHP面试题大汇总(收藏)》《php视频教程》
以上就是array_slice()函数在PHP中的正确使用的详细内容,更多请关注gxlcms其它相关文章!