Home Backend Development PHP Tutorial php 利用array_slice函数获取随机数组或前几条数据_php实例

php 利用array_slice函数获取随机数组或前几条数据_php实例

Jun 07, 2016 pm 05:11 PM

先给大家说下基本语法:

array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys ]] )

array_slice() 返回根据 offset 和 length 参数所指定的 array 数组中的一段序列。 

如果 offset 非负,则序列将从 array 中的此偏移量开始。如果 offset 为负,则序列将从 array 中距离末端这么远的地方开始。 

如果给出了 length 并且为正,则序列中将具有这么多的单元。如果给出了 length 并且为负,则序列将终止在距离数组末端这么远的地方。如果省略,则序列将从 offset 开始一直到 array 的末端。 

注意 array_slice() 默认将重置数组的键。自 PHP 5.0.2 起,可以通过将 preserve_keys 设为 TRUE 来改变此行为。

$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, );  //返回下标开始的数组 returns "c", "d", and "e"
$output = array_slice($input, -, ); // returns "d"
$output = array_slice($input, , ); // returns "a", "b", and "c"
// note the differences in the array keys
print_r(array_slice($input, , -));
print_r(array_slice($input, , -, true));
Copy after login

需要返回随机几条数据的话,可以先用shuffle($input);打乱原始数组,再用array_slice获取

PS:PHP array_slice() 函数

定义和用法

array_slice() 函数在数组中根据条件取出一段值,并返回。

注释:如果数组有字符串键,所返回的数组将保留键名。(参见例子 4)

语法

复制代码 代码如下:

array_slice(array,offset,length,preserve)

参数
描述
array
必需。规定输入的数组。
offset
必需。数值。规定取出元素的开始位置。
如果是正数,则从前往后开始取,如果是负值,从后向前取 offset 绝对值。
length
可选。数值。规定被返回数组的长度。
如果 length 为正,则返回该数量的元素。
如果 length 为负,则序列将终止在距离数组末端这么远的地方。
如果省略,则序列将从 offset 开始直到 array 的末端。
preserve
可选。可能的值:
· true - 保留键
· false - 默认 - 重置键

例子 1

<&#63;php
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1,2));
&#63;>
Copy after login

输出:

复制代码 代码如下:

Array ( [0] => Cat [1] => Horse )

例子 2

带有负的 offset 参数:

<&#63;php
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,-2,1));
&#63;>
Copy after login

输出:

复制代码 代码如下:

Array ( [0] => Horse )

例子 3

preserve 参数设置为 true:

<&#63;php
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1,2,true));
&#63;>
Copy after login

输出:

复制代码 代码如下:

Array ( [1] => Cat [2] => Horse )

例子 4

带有字符串键:

<&#63;php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse","d"=>"Bird");
print_r(array_slice($a,1,2));
&#63;>
Copy after login

输出:

复制代码 代码如下:

Array ( [b] => Cat [c] => Horse )

以上就是本文给大家介绍的php 利用array_slice函数获取数组随机或前几条数据,希望大家喜欢。

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles