PHP array_find() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_find() returns the first value in an array for which a callback function returns true. It stops searching as soon as it finds a match.

You use array_find() to pick one item from a list, such as the first number above a limit or the first active user. If nothing matches, it returns null. The callback gets the value first and the key second.

Syntax

Call array_find() with the values you need. Start with this line.

PHP
$first = array_find($numbers, fn($n) => $n > 6);
CallWhat it does
array_find([3, 8, 5, 12], fn($n) => $n > 6)Returns 8, the first number above 6.
array_find([1, 3, 5], fn($n) => $n % 2 === 0)Returns null, because no number is even.
array_find($users, fn($u) => $u['active'])Returns the first user whose 'active' field is true.

array_find() was added in PHP 8.4. If you need the key of the match and not the value, use array_find_key(). If you only need to know that a match exists, use array_any().

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

Parameters

ParameterDescription
arrayRequired. The array to search.
callbackRequired. A function that receives the value and the key, like fn($value, $key). It should return true for the item you want.

Return Value

Return ValueDescription
mixedThe first value for which the callback returned true, or null when no item matched.

PHP Examples

Example 1 — Finding the First Match

Return the first number that is greater than 6.

PHP
<?php
$numbers = [3, 8, 5, 12];

$found = array_find($numbers, fn($n) => $n > 6);

echo $found, PHP_EOL;

How It Works

  1. The callback is called with 3, which is not greater than 6.
  2. Next it is called with 8, which passes the test.
  3. array_find() stops there and never looks at 5 or 12.
  4. The value 8 is returned and printed.

Example 2 — No Match and Using the Key

When nothing matches, array_find() returns null. The callback can also check the key.

PHP
<?php
var_dump(array_find([1, 3, 5], fn($n) => $n % 2 === 0));

$letters = ['a' => 1, 'b' => 2];
var_dump(array_find($letters, fn($value, $key) => $key === 'b'));

How It Works

  1. None of 1, 3 and 5 is even, so no item passes the test.
  2. array_find() returns null and var_dump() shows NULL.
  3. In the second call the callback receives the value and then the key.
  4. Only the key 'b' passes, so its value 2 is returned.

Example 3 — Finding a Record

Search a list of records for the first one that fits a condition.

PHP
<?php
$users = [
    ['id' => 1, 'name' => 'Asha', 'active' => false],
    ['id' => 2, 'name' => 'Ravi', 'active' => true],
    ['id' => 3, 'name' => 'Meena', 'active' => true],
];

$user = array_find($users, fn($u) => $u['active']);

echo $user['name'], PHP_EOL;

How It Works

  1. Each item in $users is a record, and the callback receives one record at a time.
  2. Asha is not active, so the callback returns false for her record.
  3. Ravi is the first active user, so array_find() returns his whole record.
  4. Meena is also active, but the search had already stopped, so Ravi is returned.

Frequently Asked Questions

It returns the first value in an array for which a callback returns true. If no item matches, it returns null.
array_find() was added in PHP 8.4. On older versions you can use a foreach loop with a return or a polyfill.
Use array_find_key(). It runs the same kind of search but returns the key of the first match.
array_find() returns null in both cases. Use array_find_key(), which returns null only when nothing matched, or use array_any() first to check for a match.
array_filter() returns all matches as a new array. array_find() returns only the first match and stops looking after that.

Next: array_find_key()

array_find_key() returns the key of the first item in an array for which a callback functi.

array_find_key() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful