PHP 8 reference · Array Functions
PHP array_is_list() Function
Check whether an array's keys are 0, 1, 2… in order, so it would encode as a JSON array. Added in PHP 8.1.
PHP 8 Since 8.1
Syntax
array_is_list(array $array): bool
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
Return value
true if the keys are consecutive integers from 0.
Examples
<?php
var_dump(array_is_list(["a", "b"]));
var_dump(array_is_list([1 => "a", 2 => "b"]));
var_dump(array_is_list([]));
Output
bool(true) bool(false) bool(true)
<?php
$filtered = array_filter([1, 2, 3, 4], fn($n) => $n > 2);
var_dump(array_is_list($filtered));
echo json_encode($filtered), "\n";
Output
bool(false)
{"2":3,"3":4}Version history
| Version | Change |
|---|---|
| PHP 8.1 | Added |
Related
array_values()· Return all the values of an array as a list re-indexed from 0. Often used after array_filter() or unset().array_filter()· Keep only the elements for which a callback returns true. Without a callback it removes empty values; keys are kept.- PHP Arrays · tutorial
- PHP Array Functions · tutorial