Table of Content
You need the array_is_list function to know if an array works as a list or not in PHP.
What is the array_is_list in PHP
PHP array_is_list checks if array keys start from 0 and go up by 1.
Here is the syntax:
array_is_list(array $array): boolThis function takes one array and returns true or false.
It looks at keys and checks if they start at 0. Then it checks if the next key goes up by 1. It repeats this check for all keys in the array.
Here is a quick example:
$list = ["red", "blue", "green"];
var_dump(array_is_list($list)); // bool(true)The result is true because keys start at 0 and each next key adds 1.
The Difference Between the array_is_list and is_array
The is_array function only checks if a value is an array. The array_is_list function checks if an array has order like a list.
Here is a table that shows key differences:
| Function | What it Checks | Return Value |
|---|---|---|
| is_array | Checks if value is an array | true or false |
| array_is_list | Checks if array has keys in order like a list | true or false |
Examples of array_is_list in PHP
Simple List Array:
$fruits = ["apple", "banana", "grape"];
echo array_is_list($fruits) ? "true" : "false";// trueThis code shows true because array keys go 0,1,2 without any gap or change. Hence, the array works as a list in PHP code.
Array with Custom Keys:
$fruits = [1 => "apple", 2 => "banana", 3 => "grape"];
echo array_is_list($fruits) ? "true" : "false"; //falseThis code shows false because array keys start from 1 instead of 0. Hence, the array does not work as a list in PHP.
Mixed Keys Array:
$data = [0 => "one", 2 => "two", 3 => "three"];
echo array_is_list($data) ? "true" : "false"; // falseThis code shows false because keys break at index 1. PHP expects order with no missing steps. Hence, the function detects it is not a list.
Nested List Array:
$list = [[10, 20], [30, 40]];
echo array_is_list($list) ? "true" : "false"; // trueThis code shows true because top-level keys go 0 then 1. The function does not check inner keys, so it returns true.
Wrapping Up
In this article, you learned what array_is_list does and how it works with keys. You also saw how it differs from is_array and how to use it.
Here is a quick recap:
array_is_listchecks keys in order from 0 to n.is_arraychecks only if value is an array.- Use examples to test list or non-list arrays in your PHP code.
FAQs
What does PHP array_is_list do?
true for indexed arrays
and false for associative arrays.
$array1 = ["a", "b", "c"];
var_dump(array_is_list($array1)); // true
$array2 = [1 => "x", 2 => "y"];
var_dump(array_is_list($array2)); // false
How to check if an array is a list in PHP?
$data = ["apple", "orange", "banana"];
if (array_is_list($data)) {
echo "This is a list";
} else {
echo "This is not a list";
}
What is the difference between array_is_list and is_array?
is_array()checks if the variable is an array.array_is_list()checks if the array has sequential integer keys starting at 0.
$test = ["one", "two", "three"];
var_dump(is_array($test)); // true
var_dump(array_is_list($test)); // true
$assoc = ["a" => 1, "b" => 2];
var_dump(is_array($assoc)); // true
var_dump(array_is_list($assoc)); // false
Similar Reads
PHP developers may be need to determine the number of elements in an array. The count() function in PHP makes…
One of the important tasks is retrieving documents from a collection. MongoDB help us to make this process, but understanding…
The PHP array_diff_ukey function compares keys in two or more arrays. It returns the keys from the first array that…
let's now dive into PHP comments—these simple but super important integral parts of PHP coding. You can think of comments…
PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…
If you're coding in PHP you've most probably come across the terms 'parameters' and 'arguments' in functions. Well, they are…
The array_key_first helps you to get the first key of an array directly in PHP. It saves time and removes…
Inserting multiple data rows using PHP is a basic task. However when you start working with MySQL databases, So that…
PHP’s if-elseif statement is a fundamental construct that allows developers to control the flow of their code based on different conditions. In…
If you find a word that does not fit and want to fix it. You can use the PHP str_replace…