PHP array_is_list() Function

The array_is_list() function is an inbuilt function in PHP that is used to check whether a given array is a list or not. If the given array will be a list if the key of the array contains consecutive numbers from 0 to count($arr)-1.

Syntax: 

bool array_is_list(array $array)

Parameters: This function accepts one parameter $array that will check for the list.

Return Value: This function returns true if the given array is a list and false otherwise.

Note: This function returns true on an empty array.

Example 1:

PHP




<?php
  
$arr1 = array("10", "20", "30", "40", "50");
  
var_dump(array_is_list($arr1));
  
$arr2 = array(
    'Geeks'  => "HTML"
    'GFG'  => "CSS"
    'Geek'  => "JavaScript"
    'G4G' => "PHP"
);
  
var_dump(array_is_list($arr2));
  
?>


Output:

bool(true)
bool(false)

Example 2:

PHP




<?php
  
var_dump(array_is_list([]));
  
var_dump(array_is_list(array(
    "10" => "Geeks"
    "20" => "zambiatek",
    "30"
    "40"
    "50"
)));
  
var_dump(array_is_list(
    array(5, 10, 15, 20, 25)
));
  
?>


Output:

bool(true)
bool(false)
bool(true)

Note: This function works on PHP 8.1.0 or higher versions.

Reference: https://www.php.net/manual/en/function.array-is-list.php

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button