PHP数组对象分页方法
最近在网上看了很多的PHP数组分页的帖子,觉得有些不错!所以拿来分享一下,学习学习。
我们的数据未必都是存储在数据库中,很多时候是用数组来组织的。所以获取数组数据,进行分页是比较常见的编程要求。分页的原理很简单,就是按分页数获取某个范围的数据而已,PHP给我们提供了一个很便利的函数:array_slice()。array_slice() 函数在数组中根据条件取出一段值,并返回。
例如:
public function books($pagenum = 1) { include_once("page.php"); include("arr_books.php"); foreach ($data['books'] as $key=>$value){ $name[$key] = $value['name']; $rank[$key] = $value['rank']; } array_multisort($rank,SORT_NUMERIC,SORT_DESC,$data['books']); // 倒序 //array_multisort($rank,SORT_NUMERIC,SORT_ASC,$data['books']); // 顺序 $perpage = 10; $count = count($data['books']); $pages = new PageClass($count, $perpage, $pagenum, base_url().'veda/books/{page}/'); $start = ($pagenum - 1) * $perpage; $data['bks'] = array_slice($data['books'], $start, $perpage); $data['nav'] = $pages -> myde_write($pagenum); $this->load->view('header', $data); $this->load->view('books', $data); $this->load->view('footer', $data); }
$perpage = 2; $page = intval(getgpc('page')) ? intval($_G['gp_page']) : 1; $start = ($page - 1) * $perpage; $count = count($group_list); $list = array_slice($group_list, $start, $perpage); $multipage = multi($count, $perpage, $page, "home.php?mod=space&do=group&type=".getgpc('type'));
array_slice() 函数
array_slice() 函数在数组中根据条件取出一段值,并返回。如果数组有字符串键,所返回的数组将保留键名。
语法:array_slice(array,offset,length,preserve)
参数描述array必需。规定输入的数组。offset
必需。数值。规定取出元素的开始位置。
如果是正数,则从前往后开始取,如果是负值,从后向前取 offset 绝对值。
length
可选。数值。规定被返回数组的长度。
如果是负数,则从后向前,选取该值绝对值数目的元素。如果未设置该值,则返回所有元素。
preserve
可选。可能的值:
true - 保留键
false - 默认 - 重置键
例子 1:
<?php $a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); print_r(array_slice($a,1,2)); // Array ( [0] => Cat [1] => Horse ) ?>
例子 2(带有负的 offset 参数):
<?php $a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); print_r(array_slice($a,-2,1)); // Array ( [0] => Horse ) ?>
例子 3(preserve 参数设置为 true):
<?php $a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); print_r(array_slice($a,1,2,true)); // Array ( [1] => Cat [2] => Horse ) ?>
例子 4(带有字符串键):
<?php $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse","d"=>"Bird"); print_r(array_slice($a,1,2)); // Array ( [b] => Cat [c] => Horse ) ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
