Signature
array_any(array $array, callable $callback): boolPurpose
array_any() answers a yes-or-no question about an array: does at least one element satisfy a condition? You supply the condition as a callback. The function walks the array in order and returns true as soon as the callback accepts an element. If no element is accepted, it returns false. It is available as of PHP 8.4.0.
Before this function existed, the same check was usually written with a foreach loop and an early return, or with array_filter() followed by a count. Those approaches either take more lines or do more work than needed, because array_filter() visits every element even when the first one already answers the question.
Signature
The callback is expected to look like this:
function callback(mixed $value, mixed $key): bool
Parameters and defaults
$array: the array to search. Lists and associative arrays both work. There are no default values; both parameters are required.$callback: a callable that receives the element value as its first argument and the element key as its second. It should returntruewhen the element matches. A closure or arrow function that declares only the value parameter is fine when you do not need the key.
Return value and failure cases
The return type is always bool. You get true if the callback returned true for at least one element, and false otherwise. Once the callback returns true, iteration stops and later elements are never passed to the callback.
Passing something that is not an array, or a value that is not callable, raises a TypeError. An exception thrown inside your callback propagates out of array_any() unchanged. The function does not modify the array you pass in.
Example 1: a simple value test
The first example checks a list of animal names for long and short entries.
$animals = ['dog', 'cat', 'cow', 'duck', 'goose', 'elephant'];
var_dump(array_any($animals, fn(string $name): bool => strlen($name) > 5));
var_dump(array_any($animals, fn(string $name): bool => strlen($name) < 3));bool(true) bool(false)
Only elephant is longer than five characters, so the first call is true. No name is shorter than three characters, so the second is false.
Example 2: using the key and observing short-circuiting
The callback receives the key as its second argument. This example records each key it sees, which shows that evaluation stops at the first match.
$calls = [];
$result = array_any(
['a' => 1, 'b' => 4, 'c' => 9, 'd' => 12],
function (int $value, string $key) use (&$calls): bool {
$calls[] = $key;
return $value % 2 === 0;
}
);
echo json_encode(['result' => $result, 'calls' => $calls], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"result":true,"calls":["a","b"]}
The value 4 under key b is the first even number, so the callback is never called for c or d.
Example 3: records, empty arrays and no match
$empty = [];
$orders = [
['id' => 1, 'paid' => true],
['id' => 2, 'paid' => false],
];
echo json_encode([
'empty' => array_any($empty, fn($value): bool => true),
'unpaid' => array_any($orders, fn(array $order): bool => !$order['paid']),
'noneNegative' => array_any([3, 5, 8], fn(int $n): bool => $n < 0),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"empty":false,"unpaid":true,"noneNegative":false}
This is a typical validation pattern: "is there any unpaid order?" The callback receives each order array as the value.
Edge cases
- Empty array: the callback is never called, and the result is always
false, even if the callback would returntruefor everything. If you need the opposite behavior for empty input, usearray_all()instead. - Order matters for side effects: elements are visited in array order, so a callback with side effects runs only up to the first match.
- Keys are passed unchanged: integer keys arrive as integers and string keys as strings, so a strict
string $keytype declaration will fail on a list. - Falsy values are ordinary elements:
0,'',nullandfalseare all passed to the callback. The function tests what your callback returns, not the elements themselves.
Common mistakes
- Expecting the matching element back.
array_any()only returns a boolean. To get the element or its key, look atarray_find()andarray_find_key(). - Passing the array without a callback and expecting a truthiness check. The callback is required. Write
fn($v): bool => (bool) $vif you really want to test for any truthy element. - Writing callbacks that return non-boolean values. The documented contract is a
boolreturn, so declare: booland use comparisons to keep your intent clear. - Confusing it with
in_array(). For a plain equality check against one known value,in_array()is simpler. Usearray_any()when the condition is computed. - Relying on side effects. Because iteration stops early, counters or logs inside the callback will not cover every element.
- Assuming an empty array is a match. It never is.
Related functions
array_all()checks that every element satisfies the callback.array_filter()returns all matching elements.array_find()returns the first matching element.array_find_key()returns the key of the first matching element.
Recap
array_any() takes an array and a callback, calls the callback with each value and key in order, and returns true at the first match. It returns false when nothing matches or when the array is empty. Use it for readable existence checks, and reach for the related find functions when you need the element itself.
Tested examples
$animals = ['dog', 'cat', 'cow', 'duck', 'goose', 'elephant'];
var_dump(array_any($animals, fn(string $name): bool => strlen($name) > 5));
var_dump(array_any($animals, fn(string $name): bool => strlen($name) < 3));bool(true) bool(false)
$calls = [];
$result = array_any(
['a' => 1, 'b' => 4, 'c' => 9, 'd' => 12],
function (int $value, string $key) use (&$calls): bool {
$calls[] = $key;
return $value % 2 === 0;
}
);
echo json_encode(['result' => $result, 'calls' => $calls], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"result":true,"calls":["a","b"]}
$empty = [];
$orders = [
['id' => 1, 'paid' => true],
['id' => 2, 'paid' => false],
];
echo json_encode([
'empty' => array_any($empty, fn($value): bool => true),
'unpaid' => array_any($orders, fn(array $order): bool => !$order['paid']),
'noneNegative' => array_any([3, 5, 8], fn(int $n): bool => $n < 0),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"empty":false,"unpaid":true,"noneNegative":false}