array_first

(PHP 8 >= 8.5.0)

array_firstObtiene el primer valor de un array

Descripción

array_first(array $array): mixed

Obtiene el primer valor del array dado.

Parámetros

array
Un array.

Valores devueltos

Devuelve el primer valor de array si el array no está vacío; null en caso contrario.

Ejemplos

Ejemplo #1 Uso básico de array_first()

<?php
$array
= [1 => 'a', 0 => 'b', 3 => 'c', 2 => 'd'];

$firstValue = array_first($array);

var_dump($firstValue);
?>

El ejemplo anterior mostrará:

string(1) "a"

Ver también

add a note

User Contributed Notes 2 notes

up
4
sunny_reitgassl at hotmail dot de
25 days ago
For PHP < 8.5.0 && >= 7.3.0:

if (! function_exists("array_first")) {
    function array_first(array $array) {
        return $array ? $array[array_key_first($array)] : null;
    }
}
up
0
firejox at gmail dot com
3 days ago
There is another Polyfill for PHP < 8.5

<?php
if (!function_exists("array_first")) {
    function array_first(array $array) {
        if (!empty($array)) return current(array_slice($array, 0, 1));
        return null;
    }
}
?>
To Top