array_is_list(): checks if an array is a list

Current PHP usage

array_is_list() checks whether an array has consecutive integer keys starting at 0. It distinguishes list-shaped arrays from associative or non-consecutive arrays.

$items = ['a', 'b', 'c'];
var_dump(array_is_list($items)); // true

An empty array is also considered a list. The function checks the key structure, not the types of the stored values.

The array_is_list() function in PHP is used to determine if an array is a list. A list is an array with sequential integer keys starting from 0.

Syntax

array_is_list(array $array): bool
The function returns true if the array is a list and false otherwise.

Example 1: Simple Indexed Array

$array = [10, 20, 30]; 
$result = array_is_list($array); 
echo $result ? 'True' : 'False';
True

Example 2: Associative Array

$array = ['a' => 1, 'b' => 2]; 
$result = array_is_list($array); 
echo $result ? 'True' : 'False';
False

Example 3: Array with Mixed Keys

$array = [0 => 'apple', 'x' => 'banana', 1 => 'orange']; 
$result = array_is_list($array); 
echo $result ? 'True' : 'False';
False

Example 4: Empty Array

$array = []; 
$result = array_is_list($array); 
echo $result ? 'True' : 'False';
True

Example 5: Non-sequential Indexed Array

$array = [0 => 'apple', 2 => 'banana', 3 => 'orange']; 
$result = array_is_list($array); 
echo $result ? 'True' : 'False';
False

Example 6 : Not starting with 0 as key

$array = [1=>'Green',2=>'Red',3=>'Blue']; // Not starting with 0
$result = array_is_list($array); 
echo $result ? 'True' : 'False';
False

Example 7 : Starting from negative number

$array = [-1=>'Green',0=>'Red',1=>'Blue']; // Not starting from 0 
$result = array_is_list($array); 
echo $result ? 'True' : 'False';
False

Conclusion

The array_is_list() function helps you differentiate between indexed and associative arrays in PHP, making array manipulation more efficient and structured.
To check the variable is array or not
Array REFERENCE


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer