php function array_slice() that returns a selected part of an array

黄舟
Release: 2023-03-17 08:32:02
Original
2797 people have browsed it

Example

Start taking out the second element of the array and return all elements until the end of the array:

Copy after login

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)
Copy after login
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:
  • true - keep key names

  • false - default. Reset key name

Technical details

更多实例

实例 1

从数组的第一个元素开始取出,并返回两个元素:

Copy after login

实例 2

使用负的 start 参数:

Copy after login

实例 3

带有设置为 true 的 preserve 参数:

Copy after login

实例 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)); ?>
Copy after login

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; ?>  $v) { ?> 
name sex job
1){ echo "首页"; echo "上一页"; } if($page<$pages) { echo "下一页"; echo "末页"; } ?>
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
The preserve parameter is new in PHP 5.0.2.
Return value: Return the selected portion of the array.
PHP Version: 4+
##Update Log: