array_any
Checks whether a callback returns true for at least one element of an array, stopping at the first match.
array_any(array $array, callable $callback): bool- Added in PHP 8.4
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The array that should be searched. |
$callback | callable | Called as callback(mixed $value, mixed $key): bool for each element. If it returns true, array_any() immediately returns true and the callback is not called for further elements. |
Return value
booltrue if callback returns true for at least one element, false otherwise. An empty array returns false.
Errors
Throws a TypeError if $callback is not callable, and an ArgumentCountError if the callback declares more than two required parameters.
array_any() answers "is there at least one element like this?" It calls the callback element by element and returns true the instant one returns a truthy value; the rest of the array is never touched. If nothing matches, it returns false.
Use it whenever you only care about existence, not about which element matched or how many did. Typical checks: does this order contain a hazardous item, does this user hold any admin role, does any uploaded row have a blank identifier.
Prefer it over count(array_filter(...))
The old idiom count(array_filter($arr, $fn)) > 0 runs the callback on every single element and allocates a result array just to throw it away. array_any() does neither. For a plain value comparison, in_array() is still the simpler tool — array_any() earns its place when the test is a computed condition.
Truthiness, not identity
The return value of the callback is evaluated for truthiness, so returning a non-empty string or a positive integer counts as a match. Declare a : bool return type on your closure to keep that honest.
Example
When to use it
- Detect whether a shopping cart contains any item that requires age verification.
- Check whether any queued job has already exceeded its retry limit.
- Decide whether a diff contains any change to a file under a protected directory.
Frequently asked questions
What is the difference between array_any and in_array?
in_array() compares each element against a fixed value, while array_any() runs a callback so the test can be any expression, including a check on a nested field. Use in_array() for plain value lookups and array_any() for computed conditions.
Does array_any tell me which element matched?
No, it only returns a bool. Use array_find() to get the matching value or array_find_key() to get its key — both short-circuit the same way.
What does array_any return for an empty array?
false, because there is no element for the callback to match. This is the mirror image of array_all(), which returns true for an empty array.
More examples
Existence check on a list of records
Prints "admin present" then "no owner". This replaces a foreach with a break, and unlike in_array() it can test a field inside each nested record.
<?php
$users = [
['name' => 'ana', 'role' => 'editor'],
['name' => 'bo', 'role' => 'admin'],
];
$hasAdmin = array_any($users, fn(array $u) => $u['role'] === 'admin');
echo $hasAdmin ? "admin present\n" : "no admin\n";
$hasOwner = array_any($users, fn(array $u) => $u['role'] === 'owner');
echo $hasOwner ? "owner present\n" : "no owner\n";Cheaper than count(array_filter())
array_any() calls the callback 4 times and stops at the value 4; array_filter() calls it 100 times and builds a 97-element array before the count. Both report true, but only one of them scales.
<?php
$calls = 0;
$numbers = range(1, 100);
$test = function (int $n) use (&$calls): bool {
$calls++;
return $n > 3;
};
var_dump(array_any($numbers, $test));
echo "array_any callback calls: $calls\n";
$calls = 0;
var_dump(count(array_filter($numbers, $test)) > 0);
echo "array_filter callback calls: $calls\n";
Discussion