Home > Article > Backend Development > How to implement array paging in php
In PHP, the paging function can be implemented through the array paging function array_slice(). Its usage syntax is such as "array_slice($article,$start,$pagesize);".
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php implementation method of array paging
php Array paging array_slice() function usage
I used a function today, it is very easy to use, I would like to share it with you
from the array. In other words, you can use this function to implement paging in the same way as the sql statement. The principle is to take out the array from the queried array starting from the specified subscript to the specified length.
Our data is not necessarily stored in the database. Many times it is organized using arrays. Therefore, obtaining array data and performing paging are relatively common programming requirements
array_slice (original array, starting subscript, how many items to take), three parameters are used here (if the third parameter is not written, all elements until the end of the array are returned)
$count = count($article);//总条数 $start=($page-1)*$pagesize;//偏移量,当前页-1乘以每页显示条数 $article = array_slice($article,$start,$pagesize);
It is so simple to implement paging. It can be used when processing data in an array and needs paging
array_slice(array,start,length,preserve)
参数 | 描述 |
---|---|
array | 必需。规定数组。 |
start | 必需。数值。规定取出元素的开始位置。 0 = 第一个元素。 如果该值设置为正数,则从前往后开始取。如果该值设置为负数,则从后向前取 start 绝对值。 -2 意味着从数组的倒数第二个元素开始。 |
length | 可选。数值。规定被返回数组的长度。 如果该值设置为整数,则返回该数量的元素。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. By default, it will reorder and reset the numeric index of the array. Possible values:
|
Return Selected portion of the array. | |
---|---|
4 | |
preserve Parameters are new in PHP 5.0.2. |
PHP Video Tutorial"
The above is the detailed content of How to implement array paging in php. For more information, please follow other related articles on the PHP Chinese website!