How to implement array paging in php

藏色散人
Release: 2023-03-11 20:12:02
Original
3669 people have browsed it

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);".

How to implement array paging in php

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

array_slice () - Take out a section of

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)

Example

$count = count($article);//总条数
$start=($page-1)*$pagesize;//偏移量,当前页-1乘以每页显示条数
$article = array_slice($article,$start,$pagesize);
Copy after login

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)
Copy after login
参数描述
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:
  • true - Keep key names
  • false - Default. Reset key name
#Return value:Return Selected portion of the array. PHP Version: 4 Change Log:
preserve Parameters are new in PHP 5.0.2.
Recommended study: "

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!

Related labels:
php
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
Popular Tutorials
More>
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!