PHP array_find() Function

Quick summary: The PHP array_find() function returns the first element that satisfies a given callback.

PHP array_find() Syntax

array_find(array $array, callable $callback): mixed
PHP

PHP array_find() Basic examples

$numbers = [1, 3, 5, 8];
$result = array_find($numbers, fn($n) => $n % 2 === 0);
var_dump($result);
PHP
Output:
int(8)

Returns the first element that matches the condition.

PHP array_find() Real-world usage

$users = [['id'=>1],['id'=>2]];
$user = array_find($users, fn($u)=>$u['id']===2);
print_r($user);
PHP
Output:
Array ( [id] => 2 )

Find first matching record in dataset.

PHP array_find() Edge cases

$result = array_find([1,2,3], fn($v)=>$v>10);
var_dump($result);
PHP
Output:
NULL

Returns null if no match found.

PHP array_find() Common mistakes

Expecting index instead of value

array_find() returns value, not key.

Incorrect
$key = array_find($arr, fn($v)=>$v===5);
Correct
// use array_find_key() for key

Use array_find_key() if you need the key.

PHP array_find() Frequently Asked Questions

What does array_find() do?

Finds first matching element using a callback.

Use case?

Searching arrays.

Callback required?

Yes.

Return value?

First match or null.

Common mistake?

Assuming built-in in all PHP versions.

Performance?

Depends on array size.

Alternative?

array_filter().

Stops early?

Yes.

Safe?

Yes.

Handles objects?

Yes.

Best practice?

Use strict comparisons.

Used in filtering?

Yes.

PHP array_find() Related PHP Functions