Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

PHP array_is_list function

The PHP function array_is_list checks whether the provided array is a list. However, don’t confuse this with the list function. What it means by “a list” is whether the array is indexed and starts at 0. This way, we can use list if the array meets these criteria.

Description / Descripción
array_is_list(array $array): bool
An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1..

Parameter

$array – The array to be evaluated.

Return

Returns true if the $array is a list, and false if it is not.

Example

This example is quite comprehensive, using several array functions, including array_values to ensure that the given value becomes a list.

$auto = array(
    "marca" => "Toyota",
    "modelo" => "Corolla",
    "año" => 2023,
    "color" => "Plata",
    "precio" => 25000,
    "transmision" => "Automática",
    "motor" => "2.0L",
    "kilometraje" => 15000
);

if (array_is_list(array_values($auto))) {
    list(
        $marca,
        $modelo,
        $año,
        $color,
        $precio,
        $transmision,
        $motor,
        $kilometraje
    ) = array_values($auto);

    echo "Se puede usar list() con el array:\n";
    echo "Marca: $marca\n";
    echo "Modelo: $modelo\n";
    echo "Año: $año\n";
    echo "Color: $color\n";
    echo "Precio: $precio\n";
    echo "Transmisión: $transmision\n";
    echo "Motor: $motor\n";
    echo "Kilometraje: $kilometraje\n";
} else {
    echo "No se puede usar list() con el array.";
}
Se puede usar list() con el array:
Marca: Toyota
Modelo: Corolla
Año: 2023
Color: Plata
Precio: 25000
Transmisión: Automática
Motor: 2.0L
Kilometraje: 15000
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category