array_is_list
Checks that the keys of an array run in order from zero.
Syntax
array_is_list(array $array): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $array | array | The array to check. |
Return value
bool`true` if the array is a list.
Examples
A list and not a list:
<?php
var_dump(array_is_list(['a', 'b']));
var_dump(array_is_list(['x' => 'a']));
var_dump(array_is_list([1 => 'a', 2 => 'b']));Output:
bool(true)
bool(false)
bool(false)Notes
The function appeared in PHP 8.1. Its result decides whether json_encode() turns the array into a JSON array or an object.
See also
- array_values — Returns the values of an array with keys numbered from zero.
- is_array — Checks whether a value is an array.
Related recipes
In the tutorial
- Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.