The Problem: Finding One Item Was Clunky
Before PHP 8.4, “give me the first element that matches” had no clean answer. You either wrote a manual loop or reached for array_filter() and then threw away everything but the first result:
<?php
$users = [
['name' => 'Ada', 'active' => false],
['name' => 'Grace', 'active' => true],
['name' => 'Linus', 'active' => true],
];
// The old dance: filter everything, then grab the first key.
$matches = array_filter($users, fn($u) => $u['active']);
$first = $matches ? reset($matches) : null;
That works, but array_filter() walks the entire array even though you only needed the first hit. PHP 8.4 finally adds four native functions that do exactly what you mean — and they short-circuit.
Meet the Four New Functions
array_find($array, $callback)— first value that passes the callback, ornull.array_find_key($array, $callback)— first key that passes, ornull.array_any($array, $callback)—trueif at least one element passes.array_all($array, $callback)—trueif every element passes.
The same example becomes one readable line:
<?php
$first = array_find($users, fn($u) => $u['active']);
// => ['name' => 'Grace', 'active' => true]
if (array_any($users, fn($u) => $u['active'])) {
echo 'At least one active user';
}
if (array_all($users, fn($u) => $u['active'])) {
echo 'Everyone is active'; // not printed
}
Because they stop at the first decisive element, they are also cheaper than building a whole filtered array on large datasets.
Gotcha 1: null Is Ambiguous in array_find()
array_find() returns null both when nothing matched and when the matching element is itself null. If null is a legitimate value in your array, you cannot tell the two apart:
<?php
$values = [0, null, 42];
array_find($values, fn($v) => $v === null); // null — but it DID match!
Use array_find_key() plus array_key_exists() when the value may be null:
<?php
$key = array_find_key($values, fn($v) => $v === null);
if ($key !== null) {
echo "Found null at index $key"; // index 1
}
This works because array keys are never null — array_find_key() uses null purely as the “not found” sentinel.
Gotcha 2: Empty Arrays Give “Vacuous” Answers
array_all() on an empty array returns true, and array_any() returns false. This is mathematically correct but often surprises people:
<?php
array_all([], fn($x) => false); // true — "all" of nothing
array_any([], fn($x) => true); // false — "any" of nothing
If an empty input should be treated as an error in your domain, guard for it explicitly before calling these functions.
Gotcha 3: The Callback Gets Only the Value
Unlike array_filter() (which passes value and key), all four new callbacks receive one argument: the value. Trying to use the key in the callback will fail:
<?php
// Wrong — $key is never passed, this throws ArgumentCountError.
array_find($users, fn($value, $key) => $key === 'grace');
// Right — use array_find_key() when you care about keys.
array_find_key($users, fn($user) => $user['name'] === 'Grace');
When to Reach for Them
- Use
array_find()/array_find_key()instead ofarray_filter()+reset()when you want the first match. - Use
array_any()/array_all()instead ofcount(array_filter(...)) > 0for readable boolean checks. - Remember they need PHP 8.4+. On older runtimes, keep the loop or add a polyfill.
Small additions, but they remove a surprising amount of boilerplate from everyday backend code — and the short-circuiting is a genuine win on hot paths.