PHP array_any() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_any() checks whether at least one item in an array passes a test that you write as a callback function. It returns true as soon as the callback returns true for any item.

You use array_any() for questions like 'is there any negative number?' or 'does any user have the admin role?'. It stops at the first matching item, so it does not look at the rest of the array. The callback receives the value first and the key second.

Syntax

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

PHP
$hasEven = array_any($numbers, fn($n) => $n % 2 === 0);
CallWhat it does
array_any([1, 3, 6], fn($n) => $n % 2 === 0)Returns true because 6 is even.
array_any([1, 3, 5], fn($n) => $n % 2 === 0)Returns false because no number is even.
array_any([], fn($n) => true)Returns false because an empty array has no item to match.

array_any() was added in PHP 8.4. On older versions you can use a foreach loop with an early return. If you only need to know whether a plain value is in the array, in_array() is simpler.

Library declarations
PHP
array_any(array $array, callable $callback): bool

Parameters

ParameterDescription
arrayRequired. The array to check.
callbackRequired. A function that receives the value and the key, like fn($value, $key). It should return true when the item matches.

Return Value

Return ValueDescription
trueThe callback returned true for at least one item.
falseThe callback returned false for every item, or the array is empty.

PHP Examples

Example 1 — Looking for One Match

Pass an array and a callback. The result is true when any item passes the test.

PHP
<?php
$withEven = [1, 3, 5, 6];
$onlyOdd = [1, 3, 5];

var_dump(array_any($withEven, fn($n) => $n % 2 === 0));
var_dump(array_any($onlyOdd, fn($n) => $n % 2 === 0));

How It Works

  1. The callback fn($n) => $n % 2 === 0 is true for even numbers.
  2. $withEven contains 6, which is even, so the first call returns true.
  3. $onlyOdd has no even number, so the callback is never true.
  4. After checking every item without a match, the second call returns false.

Example 2 — Empty Arrays and Using the Key

An empty array always gives false. The callback can also use the key as its second argument.

PHP
<?php
var_dump(array_any([], fn($n) => true));

$stock = ['apple' => 0, 'pear' => 4];
var_dump(array_any($stock, fn($qty, $name) => $name === 'pear' && $qty > 0));

How It Works

  1. The empty array has no item, so even a callback that always returns true is never called.
  2. For the second array the callback receives the quantity and then the name.
  3. The item 'apple' fails because its name is not 'pear'.
  4. The item 'pear' has a quantity above 0, so array_any() returns true.

Example 3 — Stopping at the First Match

array_any() stops as soon as one item matches. A counter shows how many items were really checked.

PHP
<?php
$checked = 0;

$result = array_any([1, 2, 3, 4], function ($n) use (&$checked) {
    $checked++;
    return $n >= 2;
});

var_dump($result);
echo $checked, PHP_EOL;

How It Works

  1. The callback adds 1 to $checked every time it runs.
  2. The value 1 does not match because it is less than 2.
  3. The value 2 matches, so array_any() stops and never looks at 3 or 4.
  4. The result is true and the callback ran exactly 2 times.

Frequently Asked Questions

It returns true when a callback returns true for at least one item in the array, and false when no item matches. It is a short way to ask whether any item fits a rule.
array_any() was added in PHP 8.4. It is not available in PHP 8.3 or older, so use a foreach loop or a polyfill there.
It returns false, because there is no item that could match the callback.
in_array() compares a plain value with the items. array_any() runs your own callback, so it can test any condition, such as a number greater than 10 or a record with a certain field.
Use array_find() to get the first matching value, or array_find_key() to get its key. array_any() only answers yes or no.

Next: array_change_key_case()

array_change_key_case() returns a copy of an array with all string keys changed to lowerca.

array_change_key_case() 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