Sometimes you need to check whether at least one element in a PHP array meets a specific condition. For example, you might want to know if an order contains an unavailable product or if a list contains a negative number.
PHP provides the array_any() function to perform these checks without writing a separate loop.
In this guide, you'll learn how array_any() works, how to use callbacks, and how to apply it in practical situations.
PHP version requirement: The array_any() function was introduced in PHP 8.4. It is not available in earlier PHP versions.
What Is array_any() in PHP?
The array_any() function checks whether at least one array element satisfies a condition defined by a callback function.
Its syntax is:
array_any(array $array, callable $callback): bool
It accepts two arguments:
$array— The array containing the values to check.$callback— A function that evaluates each element and determines whether it matches the condition.
The callback receives the current element's value and its key. You can omit the key parameter if you don't need it.
The function returns true as soon as an element passes the test. If no element matches, it returns false.
Example 1: Checking for Negative Numbers
Let's check whether an array contains at least one negative number.
<?php
$numbers = [10, 25, -4, 18];
$hasNegative = array_any(
$numbers,
fn(int $number): bool => $number < 0
);
var_dump($hasNegative);
Output:
bool(true)
The callback checks whether each number is smaller than zero.
Because the array contains -4, array_any() returns true.
If every number were positive, the result would be false.
Example 2: Checking Associative Array Keys and Values
The callback can also inspect an array element's key.
Imagine an inventory array containing product names and available quantities.
<?php
$stock = [
"Keyboard" => 12,
"Mouse" => 3,
"Monitor" => 8
];
$result = array_any(
$stock,
fn(int $quantity, string $product): bool =>
$product === "Mouse" && $quantity < 5
);
var_dump($result);
Output:
bool(true)
The callback receives two values:
$quantitycontains the current stock quantity.$productcontains the corresponding array key.
The result is true because the Mouse has fewer than five units available.
How Does array_any() Handle Empty Arrays?
When the input array is empty, array_any() returns false.
<?php
$numbers = [];
$result = array_any(
$numbers,
fn(int $number): bool => $number > 100
);
var_dump($result);
Output:
bool(false)
Since there are no elements to check, none can satisfy the condition.
This differs from array_all(), which returns true for an empty array.
Short-Circuit Evaluation
One useful feature of array_any() is that it stops checking elements as soon as it finds a match.
Consider this example:
<?php
$numbers = [5, 12, 30, 45];
$result = array_any(
$numbers,
function (int $number): bool {
echo "Checking: $number" . PHP_EOL;
return $number > 20;
}
);
var_dump($result);
Output:
Checking: 5
Checking: 12
Checking: 30
bool(true)
PHP stops after finding 30 because it satisfies the condition.
The final number, 45, is never checked.
This behavior is called short-circuit evaluation. It avoids unnecessary work once the answer is known.
Practical Example: Checking Product Availability
Let's build a simple shopping cart example.
Each item contains a requested quantity and the number of units available in stock.
We'll use array_any() to check whether any requested quantity exceeds the available stock.
Create a file named check-cart.php:
<?php
declare(strict_types=1);
$cart = [
[
"product" => "Keyboard",
"quantity" => 2,
"available" => 10
],
[
"product" => "Mouse",
"quantity" => 5,
"available" => 3
],
[
"product" => "Monitor",
"quantity" => 1,
"available" => 4
]
];
// Check whether any item exceeds stock.
$hasStockProblem = array_any(
$cart,
fn(array $item): bool =>
$item["quantity"] > $item["available"]
);
if ($hasStockProblem) {
echo "Some products have insufficient stock.";
} else {
echo "All products are available.";
}
Output:
Some products have insufficient stock.
The Mouse has only three units available, but the customer requested five.
Because at least one item fails the availability check, array_any() returns true.
This example uses predefined, correctly structured data. In a real application, validate incoming quantities and product information before performing stock checks.
array_any() vs array_all() vs array_filter()
| Function | Purpose |
|---|---|
array_any() |
Returns true if at least one element matches. |
array_all() |
Returns true if every element matches. |
array_filter() |
Returns an array containing elements that pass the filter. |
Choose array_any() when you only need to know whether a matching element exists.
If you need the matching elements themselves, array_filter() or another suitable search function may be more appropriate.
Using PHP Versions Earlier Than 8.4
If you're using PHP 8.3 or earlier, you can achieve the same behavior using foreach.
<?php
$numbers = [10, 25, -4, 18];
$hasNegative = false;
foreach ($numbers as $number) {
if ($number < 0) {
$hasNegative = true;
break;
}
}
var_dump($hasNegative);
Output:
bool(true)
The loop stops at the first matching element, just like array_any().
Common Mistakes
- Using an older PHP version:
array_any()requires PHP 8.4 or later. - Confusing any with all:
array_any()requires only one matching element, not every element. - Expecting the matching value: The function returns a boolean, not an array element.
- Assuming callbacks always run for every element: Checking stops as soon as a match is found.
- Ignoring input types: Validate array values before using typed callbacks with data from external sources.
Key Takeaways
array_any()was introduced in PHP 8.4.- It checks whether at least one array element meets a condition.
- The callback receives the current value and optionally its key.
- The function returns
truewhen a matching element is found. - Empty arrays return
false. - Evaluation stops immediately after the first match.
- Use a
foreachloop for equivalent functionality in older PHP versions.
The array_any() function provides a convenient way to check for matching conditions in PHP arrays. It's particularly useful for detecting invalid records, identifying unavailable products, and checking whether a collection contains at least one matching value.
For additional details, see the official PHP array_any() documentation.