PHP 8.5.6 Released!

array_first

(PHP 8 >= 8.5.0)

array_firstRenvoie la première valeur d'un tableau

Description

array_first(array $array): mixed

Renvoie la première valeur du array donné.

Liste de paramètres

array
Un tableau.

Valeurs de retour

Renvoie la première valeur du array donné si le tableau n'est pas vide; sinon null.

Exemples

Exemple #1 Utilisation basique de array_first()

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

$firstValue = array_first($array);

var_dump($firstValue);
?>

L'exemple ci-dessus va afficher :

string(1) "a"

Voir aussi

add a note

User Contributed Notes 2 notes

up
4
sunny_reitgassl at hotmail dot de
28 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
6 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