PHP array_is_list() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_is_list() checks whether an array is a list, meaning its keys are 0, 1, 2 and so on in order.

It returns true for a normal numbered array and false when keys are strings, skipped or out of order. Available since PHP 8.1.

Syntax

Call array_is_list() with the values you need. Start with this line.

PHP
$isList = array_is_list($items);
CallWhat it does
array_is_list(["a", "b", "c"])Returns true because the keys are 0, 1, 2.
array_is_list(["x" => 1])Returns false because the key is a string.
array_is_list([])Returns true. An empty array counts as a list.

This function needs PHP 8.1 or newer. On older versions you would compare array_keys($array) with range(0, count($array) - 1).

Library declarations
PHP
array_is_list(array $array): bool

Parameters

ParameterDescription
arrayRequired. The array you want to check.

Return Value

Return ValueDescription
booltrue if the keys are exactly 0, 1, 2, ... in order, otherwise false.

PHP Examples

Example 1 — List vs Associative Array

Check a normal array and an array with named keys.

PHP
<?php
$a = [1, 2, 3];
$b = ["a" => 1, "b" => 2];

echo array_is_list($a) ? "list" : "not a list", PHP_EOL;
echo array_is_list($b) ? "list" : "not a list", PHP_EOL;

How It Works

  1. Create a numbered array. Its keys are automatically 0, 1 and 2.
  2. Create an array with names. Its keys are the strings a and b.
  3. Call array_is_list() on each. The first returns true, the second false.
  4. Use a ternary. It prints a friendly word instead of 1 or an empty string.

Example 2 — Empty, Skipped and Reordered Keys

Only keys that start at 0 and go up by one, in order, make a list.

PHP
<?php
echo array_is_list([]) ? "list" : "not a list", PHP_EOL;
echo array_is_list([1 => "a", 2 => "b"]) ? "list" : "not a list", PHP_EOL;
echo array_is_list([1 => "b", 0 => "a"]) ? "list" : "not a list", PHP_EOL;

How It Works

  1. Check an empty array. It has no keys that break the rule, so it is a list.
  2. Start keys at 1. A list must start at 0, so this fails.
  3. Reverse the key order. The keys are 1 then 0, which is not the right order.
  4. Remember the rule. Keys must be exactly 0, 1, 2 ... in that order.

Example 3 — Fix a Gap After unset()

Removing an item makes the array stop being a list until you renumber it.

PHP
<?php
$items = ["a", "b", "c"];
echo array_is_list($items) ? "list" : "not a list", PHP_EOL;

unset($items[1]);
echo array_is_list($items) ? "list" : "not a list", PHP_EOL;

$items = array_values($items);
echo array_is_list($items) ? "list" : "not a list", PHP_EOL;

How It Works

  1. Start with three items. The keys are 0, 1 and 2, so it is a list.
  2. Remove the middle item. unset() leaves keys 0 and 2 with a gap.
  3. Check again. The gap means it is no longer a list.
  4. Renumber with array_values(). Keys become 0 and 1, and it is a list again.

Frequently Asked Questions

It checks whether an array has the keys 0, 1, 2 ... in order. It returns true for such a list and false otherwise.
It was added in PHP 8.1. On older versions it does not exist, so you need your own check.
Yes. array_is_list([]) returns true.
unset() removes the key but does not renumber the others. Call array_values() to rebuild the keys from 0.
It helps you decide whether to treat data as a JSON array or object, or whether a loop by index is safe.

Next: array_key_exists()

array_key_exists() checks whether a given key exists in an array.

array_key_exists() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful