php数组函数序列之array_slice()
array_slice()定义和用法
array_slice() 函数在数组中根据条件取出一段值,并返回。
注释:如果数组有字符串键,所返回的数组将保留键名。(参见例子 4)
语法
array_slice(array,offset,length,preserve)参数 描述
array 必需。规定输入的数组。
offset 必需。数值。规定取出元素的开始位置。
如果是正数,则从前往后开始取,如果是负值,从后向前取 offset 绝对值。
length 可选。数值。规定被返回数组的长度。
如果是负数,则从后向前,选取该值绝对值数目的元素。如果未设置该值,则返回所有元素。
preserve 可选。可能的值:
true - 保留键
false - 默认 - 重置键
例子 1
复制代码 代码如下:
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1,2));
?>
输出:
Array ( [0] => Cat [1] => Horse )例子 2
带有负的 offset 参数:
复制代码 代码如下:
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,-2,1));
?>
输出:
Array ( [0] => Horse )例子 3
preserve 参数设置为 true:
复制代码 代码如下:
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1,2,true));
?>
输出:
Array ( [1] => Cat [2] => Horse )例子 4
带有字符串键:
复制代码 代码如下:
$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

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

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor
