Arrays7 min readExamples run on PHP 8.5.10

array_any()

Learn how PHP 8.4 array_any() tests whether at least one array element satisfies a callback, with examples, edge cases and common mistakes.

Signature

array_any(array $array, callable $callback): bool
Available since PHP 8.4.07.48.08.18.28.38.48.5All versions →

Purpose

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 return true when 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.

Example
<?php
$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));
Output
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.

Example
<?php
$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";
Output
{"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

Example
<?php
$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";
Output
{"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 return true for everything. If you need the opposite behavior for empty input, use array_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 $key type declaration will fail on a list.
  • Falsy values are ordinary elements: 0, '', null and false are all passed to the callback. The function tests what your callback returns, not the elements themselves.

Common mistakes

  1. Expecting the matching element back. array_any() only returns a boolean. To get the element or its key, look at array_find() and array_find_key().
  2. Passing the array without a callback and expecting a truthiness check. The callback is required. Write fn($v): bool => (bool) $v if you really want to test for any truthy element.
  3. Writing callbacks that return non-boolean values. The documented contract is a bool return, so declare : bool and use comparisons to keep your intent clear.
  4. Confusing it with in_array(). For a plain equality check against one known value, in_array() is simpler. Use array_any() when the condition is computed.
  5. Relying on side effects. Because iteration stops early, counters or logs inside the callback will not cover every element.
  6. Assuming an empty array is a match. It never is.
  • 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

Testing values with an arrow function
<?php
$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));
Output
bool(true)
bool(false)
Using the key and observing short-circuiting
<?php
$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";
Output
{"result":true,"calls":["a","b"]}
Records, empty arrays and no match
<?php
$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";
Output
{"empty":false,"unpaid":true,"noneNegative":false}