Oihana PHP

find.php

Table of Contents

Functions

find()  : mixed
Returns the first element of an array that satisfies a predicate.

Functions

find()

Returns the first element of an array that satisfies a predicate.

find(array<int|string, mixed> $items, callable $predicate[, mixed $default = null ]) : mixed

The $predicate callback is invoked as fn( $value , $key ) for every entry, in iteration order, until it returns a truthy value. If no element matches, the $default value is returned.

Parameters
$items : array<int|string, mixed>

The array to search.

$predicate : callable

The test callback: fn( $value , $key ): bool.

$default : mixed = null

The value returned when no element matches. Default null.

Tags
example
use function oihana\core\arrays\find;

$first = find( [ 1 , 3 , 4 , 7 ] , fn( $n ) => $n % 2 === 0 ) ;
// 4

$none = find( [ 1 , 3 ] , fn( $n ) => $n > 10 , -1 ) ;
// -1
author

Marc Alcaraz (ekameleon)

since
1.0.9
Return values
mixed

The first matching value, or $default.

On this page

Search results