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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_is_list(array $array): bool

Parameters

ParameterDescription
array The input array.

Return value

true if the keys are consecutive integers from 0.

Examples

Lists and mapsTry it yourself
<?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)
Why filtered arrays become JSON objectsTry it yourself
<?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

VersionChange
PHP 8.1Added

Was this page helpful?