array_all
Checks whether a callback returns true for every element of an array, short-circuiting on the first failure.
array_all(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 false, array_all() immediately returns false and the callback is not called for further elements. |
Return value
booltrue if callback returns true for all elements, false otherwise. An empty array returns true.
Errors
Throws a TypeError if $callback is not callable, and an ArgumentCountError if the callback declares more than two required parameters.
array_all() is a universal quantifier: it walks the array and returns true only if the callback returns a truthy value for every element. The moment the callback returns false, iteration stops and the function returns false — the remaining elements are never visited.
Reach for it wherever you would otherwise write a foreach with a flag variable and an early return false. Validation is the classic case: every required field is filled, every imported row parses, every line item has a positive quantity.
The callback receives the key too
The callback is invoked as callback($value, $key). PHP lets a userland callback accept fewer arguments than it is handed, so a one-parameter closure works fine; declare the second parameter only when you actually need the key.
Empty arrays are true
An empty array vacuously satisfies every condition, so array_all([], $fn) is true. That is mathematically correct but rarely what a validation routine wants, so pair it with an emptiness check when "all" should also mean "at least one".
Before PHP 8.4 the common workaround was !in_array(false, array_map($fn, $arr), true), which builds an entire intermediate array and never short-circuits.
Example
When to use it
- Confirm every row of an uploaded CSV has the expected number of columns before importing it.
- Verify a cart contains only in-stock items before starting checkout.
- Assert that every value in a config array is a non-empty string during bootstrap.
Frequently asked questions
What is the difference between array_all and array_any?
array_all() returns true only when the callback is true for every element, while array_any() returns true when it is true for at least one. They also disagree on empty input: array_all([]) is true and array_any([]) is false.
Does array_all check every element?
No. It stops at the first element whose callback returns false, so a callback with side effects such as logging will not run for the remaining elements.
How do I use array_all in PHP 8.3 or older?
Write a small polyfill guarded by function_exists(): loop the array with foreach and return false on the first failing element. Avoid the array_map()/in_array() trick, since it evaluates the callback for every element.
More examples
Using the key argument
The callback is called with both the value and the key, so the first check inspects only keys and the second only values. All three checks pass, so each var_dump prints bool(true).
<?php
$row = ['sku' => 'A-1', 'qty' => 3, 'price' => 9.5];
$required = ['sku', 'qty', 'price'];
$allStringKeys = array_all($row, fn($value, $key) => is_string($key));
var_dump($allStringKeys);
$noNulls = array_all($row, fn($value, $key) => $value !== null);
var_dump($noNulls);
var_dump(array_all($required, fn($f) => array_key_exists($f, $row)));Short-circuiting is observable
The echo runs for 2, 4 and 5 only. As soon as 5 fails the test array_all() stops, so 6 and 8 are never checked and the final line prints bool(false).
<?php
$numbers = [2, 4, 5, 6, 8];
$result = array_all($numbers, function (int $n): bool {
echo "checking $n\n";
return $n % 2 === 0;
});
var_dump($result);
Discussion