PHP array_is_list: Check if an Array is a List with Examples

php array is list

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): bool

This 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:

FunctionWhat it ChecksReturn Value
is_arrayChecks if value is an arraytrue or false
array_is_listChecks if array has keys in order like a listtrue or false

Examples of array_is_list in PHP

Simple List Array:

$fruits = ["apple", "banana", "grape"];
echo array_is_list($fruits) ? "true" : "false";// true

This 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"; //false

This 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"; // false

This 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"; // true

This 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_list checks keys in order from 0 to n.
  • is_array checks 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?

PHP array_is_list checks if an array has consecutive integer keys starting from zero. The function returns 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?

You can use array_is_list to check if an array is a list. It works on PHP 8.1 or higher.

$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 array_multisort Function with Examples

PHP array_multisort sorts many arrays or a multi level array in one go. It can sort data in numeric order…

PHP SimpleXML: Work with XML in Examples

PHP SimpleXML - invented for you to work with XML. Way leaner, quicker to get up. it's already integrated directly…

PHP array_chunk(): Split Arrays into Manageable Parts

When working with big arrays in PHP, things can get messy. That is where php array_chunk() steps in by providing…

PHP filter_list(): List Available Filters

PHP introduces the filter_list() function to give developers a way to check all available filters in the filter extension. This…

PHP array_shift: Remove the First Array Element with Examples

The array_shift function in PHP takes the first element from an array and returns it. It also moves all other…

PHP is_dir Function: Check if a Directory Exists

Sometimes, when you run a script, you may encounter an error because the directory you're trying to access doesn't exist.…

PHP Bitwise Operators: AND, OR, XOR, NOT with Examples

Bitwise operators use symbols like &, |, and ^, and they work at the binary level. But once you understand…

Understanding the PHP Exclusive OR (XOR) Operator

In PHP, there is this interesting operator known as "exclusive OR," or just XOR. It is somewhat of an underdog…

PHP Namespace: How to Group Code (With Example)

PHP namespace solves the problem of name conflicts. Different developers may create functions-classes, or constants with the same name. PHP…

PHP filter_input_array: How to Clean and Validate Input

The filter_input_array() filters multiple inputs in PHP at once. It helps you to clean and validate data. Understand the filter_input_array…

Previous Article

PHP array_diff: How it Works with Examples

Next Article

How to Use sub Tag for Subscript Text in HTML

Write a Comment

Leave a Comment

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


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.