Home > Backend Development > PHP Tutorial > php怎么取出数组内指定的值

php怎么取出数组内指定的值

PHPz
Release: 2020-06-24 11:06:50
Original
4258 people have browsed it

php怎么取出数组内指定的值

php取出数组内指定的值

PHP array_slice() 函数

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

注释:如果数组有字符串键,所返回的数组将保留键名。

语法

array_slice(array,offset,length,preserve)
Copy after login

参数描述array必需。规定输入的数组。offset

必需。数值。规定取出元素的开始位置。

如果是正数,则从前往后开始取,如果是负值,从后向前取 offset 绝对值。

length

可选。数值。规定被返回数组的长度。

如果 length 为正,则返回该数量的元素。

如果 length 为负,则序列将终止在距离数组末端这么远的地方。

如果省略,则序列将从 offset 开始直到 array 的末端。

preserve

可选。可能的值:

true - 保留键

false - 默认 - 重置键

//数组如下

$arr =array(
                '0 '=>'1',
                '1' =>2,
               ' 2' =>3,
                '3' =>4,
                '4' =>5,
                '5' =>6,
                '6' =>7,
                '7' =>8,
                '8' =>9,
                '9' => 'fhksadhfj',
                '10' => 19
            )
//保留原键值
$a = array_slice($arr,4,10,ture);
print_r($a);
Copy after login

结果输出如下:

Array(    [4] => 5    [5] => 6    [6] => 7    [7] => 8    [8] => 9    [9] => fhksadhfj    [10] => 19)
//重置键值
$a = array_slice($arr,4,10,false);
print_r($a);
Copy after login

结果输出如下:

Array(    [0] => 5    [1] => 6    [2] => 7    [3] => 8    [4] => 9    [5] => fhksadhfj    [6] => 19)
Copy after login

更多相关技术文章,请访问PHP中文网

Related labels:
source:php.cn
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template