PHP 8.6.0 Beta 1 is available for testing

array_is_list

(PHP 8 >= 8.1.0)

array_is_listVerifica si un array dado es una lista

Descripción

function array_is_list(array $array): bool

Determina si el array dado es una lista. Un array se considera como una lista si sus claves están compuestas por números consecutivos de 0 a count($array)-1.

Parámetros

array

El array en evaluación.

Valores devueltos

Devuelve true si array es una lista, de lo contrario false .

Ejemplos

Ejemplo #1 Ejemplo de array_is_list()

<?php
var_dump(array_is_list([])); // true
var_dump(array_is_list(['apple', 2, 3])); // true
var_dump(array_is_list([0 => 'apple', 'orange'])); // true
// El array no comienza en 0
var_dump(array_is_list([1 => 'apple', 'orange'])); // false
// Las claves no están en el orden correcto
var_dump(array_is_list([1 => 'apple', 0 => 'orange'])); // false
// Claves no enteras
var_dump(array_is_list([0 => 'apple', 'foo' => 'bar'])); // false
// Claves no consecutivas
var_dump(array_is_list([0 => 'apple', 2 => 'bar'])); // false
?>

Notas

Nota:

Esta función devuelve true para los arrays vacíos.

Véase también

Found A Problem?

Learn How To Improve This PageSubmit a Pull RequestReport a Bug
+add a note

User Contributed Notes 6 notes

up
60
phpnet at jesseschalken dot com
4 years 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;
 }
}
?>
up
26
divinity76+spam at gmail dot com
4 years 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)
up
16
Matteo Galletti
4 years 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));
 }
}
?>
up
1
maruerru
2 years ago
I think, this is the fastest polyfill:
<?php
if (!function_exists('array_is_list')) {
 function array_is_list(array $array)
 {
 return $array === array_values($array);
 }
}
?>
up
0
info at ensostudio dot ru
4 years 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);
 }
}
?>
up
-5
2317216477 at qq dot com
1 year ago
function isIndexed($array)
{
 if (is_array($array)) {
 $keys = array_keys($array);
 return $keys === array_keys($keys);
 }
 return false;
}
function isAssoc($array)
{
 if (is_array($array)) {
 $keys = array_keys($array);
 return $keys !== array_keys($keys);
 }
 return false;
}
these two function is more faster than divinity76+spam at gmail dot com's about three times in my computer for PHP 8.2.9:
array(3) {
 ["pre"]=>
 float(0.8179779052734375)
 ["post"]=>
 float(0.8116860389709473)
 ["index"]=>
 float(0.3369460105895996)
}
:)
+add a note

AltStyle によって変換されたページ (->オリジナル) /