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.
The array_is_list() function is available in PHP 8.1 and later versions only.
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