php 利用array_slice函数获取随机数组或前几条数据_php实例
先给大家说下基本语法:
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));
需要返回随机几条数据的话,可以先用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
<?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 )
以上就是本文给大家介绍的php 利用array_slice函数获取数组随机或前几条数据,希望大家喜欢。

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

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

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



Alipay PHP...

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,

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 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? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

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

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�...

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.
