Example
Start taking out the second element of the array and return all elements until the end of the array:
Definition and usage
array_slice() function return Selected portion of the array.
Note: If the array hasstringkeys, the returned array will retain the keys (see Example 4).
Syntax
array_slice(array,start,length,preserve)
Parameters | Description |
array | Required. Specifies an array. |
start | Required. numerical value. Specifies the starting position of the element to be retrieved. 0 = first element. If the value is set to a positive number, it will be taken from front to back. If the value is set to a negative number, the absolute value of start is taken from back to front. -2 means start from the second to last element of the array. |
length | Optional. numerical value. Specifies the length of the returned array. If the value is set to an integer, that number of elements is returned. If this value is set to a negative number, the function will terminate fetching this far from the end of the example array. If this value is not set, all elements starting from the position set by the start parameter to the end of the array are returned. |
preserve | Optional. Specifies whether the function retains key names or resets key names. Possible values:
|
Technical details
更多实例
实例 1
从数组的第一个元素开始取出,并返回两个元素:
实例 2
使用负的 start 参数:
实例 3
带有设置为 true 的 preserve 参数:
实例 4
带有字符串和整数键名:
"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_slice函数实现的分页方法非常好用,分享如下:
'name1','sex' => 'sex1','job' => 'job1'), array('name'=> 'name2','sex' => 'sex2','job' => 'job2'), array('name'=> 'name3','sex' => 'sex3','job' => 'job3'), array('name'=> 'name4','sex' => 'sex4','job' => 'job4'), array('name'=> 'name5','sex' => 'sex5','job' => 'job5'), array('name'=> 'name6','sex' => 'sex6','job' => 'job6'), array('name'=> 'name7','sex' => 'sex7','job' => 'job7'), array('name'=> 'name8','sex' => 'sex8','job' => 'job8'), array('name'=> 'name9','sex' => 'sex9','job' => 'job9'), array('name'=> 'name10','sex' => 'sex10','job' => 'job10'), array('name'=> 'name11','sex' => 'sex11','job' => 'job11'), array('name'=> 'name12','sex' => 'sex12','job' => 'job12'), ); //计算总记录条数 $num = count($arr); //规定每页显示的条数 $perpage = 3; //计算页数 $pages = ceil($num/$perpage); //echo $num,$perpage,$pagecount;exit; if(is_numeric($_REQUEST['page'])) { if($_REQUEST['page']<1){ $page = 1; }elseif($_REQUEST['page']>$pages) { $page = $pages; }else{ $page = $_REQUEST['page']; } }else{ $page = 1; } $start = ($page-1)*$perpage; $newpage = array_slice($arr,$start,$perpage,true); //print_r($newpage);exit; ?>
name | sex | job |
The above is the detailed content of php function array_slice() that returns a selected part of an array. For more information, please follow other related articles on the PHP Chinese website!
Return value: | Return the selected portion of the array. |
PHP Version: | 4+ |
##Update Log: | The preserve parameter is new in PHP 5.0.2.