Home Backend Development PHP Tutorial PHP数组对象分页方法

PHP数组对象分页方法

Jun 20, 2016 pm 01:01 PM
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);
}
Copy after login
$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'));
Copy after login

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

例子 2(带有负的 offset 参数):

<?php $a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,-2,1));
// Array ( [0] => Horse )
?>
Copy after login

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

例子 4(带有字符串键):

<?php $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse","d"=>"Bird");
print_r(array_slice($a,1,2));
// Array ( [b] => Cat [c] => Horse )
?>
Copy after login


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles