Arr::first

Posted on November 16, 2016

The Arr::first function returns the first element of an array passing a given truth test.

A default value may also be passed as the third parameter to the method. This value will be returned if no value passes the truth test:

$value = Arr::first($array, $callback, $default);

Example usage

$array = [100, 200, 300];

$value = Arr::first(
    [100, 200, 300],
    function ($key, $value) {
        return $value >= 150;
    }
);

$value2 = Arr::first(
    [100, 200, 300]
);

// 200
// 100

Parameters

array $array

callable|null $callback

mixed $default

Return Value

mixed Return the first element in an array passing a given truth test.

0 Comments

Leave a Reply

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