这篇文章主要介绍了php 利用array_slice函数获取随机数组或前几条数据的相关资料,需要的朋友可以参考下
先给大家说下基本语法:
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 - 保留键
例子 1
"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1,2));
?>
输出:
复制代码 代码如下:
Array ( [0] => Cat [1] => Horse )
例子 2
带有负的 offset 参数:
"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); print_r(array_slice($a,-2,1)); ?>
输出:
复制代码 代码如下:
Array ( [0] => Horse )
例子 3
preserve 参数设置为 true:
"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); print_r(array_slice($a,1,2,true)); ?>
输出:
复制代码 代码如下:
Array ( [1] => Cat [2] => Horse )
例子 4
带有字符串键:
"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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.