array_is_list
(PHP 8 >= 8.1.0)
array_is_list — 判断给定的 array
是否为 list
说明
array_is_list(array
$array
): bool
判断指定的 array
是否是 list。如果 array 的 key 由 0
到 count($array)-1
的连续数字组成,则该数组就是 list。
参数
-
array
-
被检测的 array。
返回值
如果 array
是 list 就返回 true
,否则返回 false
。
范例
示例 #1 array_is_list() 示例
<?php
array_is_list([]); // true
array_is_list(['apple', 2, 3]); // true
array_is_list([0 => 'apple', 'orange']); // true
// key 未从 0 开始
array_is_list([1 => 'apple', 'orange']); // false
// key 的顺序不正确
array_is_list([1 => 'apple', 0 => 'orange']); // false
// 包含非整数 key
array_is_list([0 => 'apple', 'foo' => 'bar']); // false
// 非连续 key
array_is_list([0 => 'apple', 2 => 'bar']); // false
?>
注释
注意:
空数组也会返回
true
。

User Contributed Notes 5 notes
phpnet at jesseschalken dot com ¶
4 months ago
Polyfills that call `array_keys()`, `array_values()` or `range()` are inefficient because they create new arrays unnecessarily.
Please use this polyfill instead which creates no new arrays and only does a single pass over the given array.
<?php
if (!function_exists("array_is_list")) {
function array_is_list(array $array): bool
{
$i = 0;
foreach ($array as $k => $v) {
if ($k !== $i++) {
return false;
}
}
return true;
}
}
?>
divinity76+spam at gmail dot com ¶
1 month ago
slightly optimized version of phpnet at jesseschalken dot com's excellent array_is_list:
<?php
if (!function_exists("array_is_list")) {
function array_is_list(array $array): bool
{
$i = -1;
foreach ($array as $k => $v) {
++$i;
if ($k !== $i) {
return false;
}
}
return true;
}
}
?>
benchmarks: https://3v4l.org/9BPqL
why is this faster you ask? because post-increment does more,
here is what pre-increment actually means:
step 1: increment the value by 1.
step 2: return the value.
here is what post-increment actually means:
step 1: create a copy of the original value.
step 2: increment the original value by 1.
step 3: return the copy.
another question might be "why didn't you write `if ($k !== ++$i) {` ? ... that is a good question! turns out that ++$i;if($k!==$i){...} is faster on PHP7 than if($k !== ++$i){...} for reasons unknown to me.. (if you have an answer, feel free to email me about it!)
(i have NOT checked if PHP8/jit auto-optimize this stuff, but at least back in PHP7 it's true that pre-increment is faster than post-increment, and this polyfill is primarily for PHP7)
Matteo Galletti ¶
7 months ago
Polyfill implementation for PHP versions lower than 8.1.
<?php
if (!function_exists('array_is_list'))
{
function array_is_list(array $a)
{
return $a === [] || (array_keys($a) === range(0, count($a) - 1));
}
}
?>
info at ensostudio dot ru ¶
7 months ago
old school polyfill (:
<?php
if (!function_exists('array_is_list')) {
function array_is_list(array $array)
{
if ($array === []) {
return true;
}
$keys = array_keys($array);
return $keys === array_keys($keys);
}
}
?>
iradu at unix-world dot org ¶
4 months ago
// How about:
<?php
if(count(array_filter(array_keys($y_arr), 'is_string')) === 0) {
echo 'non-associative ; is list';
} else {
echo 'associative ; non list'
}
备份地址:http://www.lvesu.com/blog/php/function.array-is-list.php