array_is_list(): checks if an array is a list

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


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





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