Signature
array_all(array $array, callable $callback): boolPurpose
array_all() answers one question: does every element of an array satisfy a rule? You supply the array and a callback. The function runs the callback on the elements in order and returns true only if the callback accepts all of them. It is available starting with PHP 8.4.0.
Before this function you would write a foreach loop with an early return false, or compare the result of array_filter() with the original array. array_all() states the intent directly and stops as soon as the answer is known.
Signature
The callback has this shape:
callback(mixed $value, mixed $key): bool
Parameters
$array: the array to check. Both lists and associative arrays work. There is no default value, and the argument is required.$callback: a callable that receives the element value first and the element key second. It should returntruewhen the element is acceptable andfalsewhen it is not. A callback that only needs the value can declare a single parameter, since PHP lets you ignore extra arguments passed to a user-defined function.
Return value and failure cases
The function returns a bool:
trueif the callback returnstruefor every element.falseas soon as the callback returnsfalsefor one element. The callback is not called for the remaining elements.
Passing something that is not an array, or something that is not callable, raises a TypeError. Exceptions thrown inside your callback propagate out of array_all() unchanged.
Example 1: checking a rule on values
The first example checks whether all scores reach two different thresholds.
$scores = [82, 91, 77, 100];
var_dump(array_all($scores, fn(int $score): bool => $score >= 70));
var_dump(array_all($scores, fn(int $score): bool => $score >= 80));bool(true) bool(false)
Every score is at least 70, so the first call returns true. The score 77 fails the second rule, so that call returns false.
Example 2: keys and short-circuiting
The callback receives the key as its second argument, and evaluation stops at the first failure. This example records which keys were visited.
$calls = [];
$items = ['a' => 3, 'b' => 8, 'c' => 5, 'd' => 9];
$result = array_all($items, function (int $value, string $key) use (&$calls): bool {
$calls[] = $key;
return $value < 6;
});
echo json_encode(['result' => $result, 'calls' => $calls], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
echo "\n";{"result":false,"calls":["a","b"]}
The value for b is 8, which fails the rule. The function returns false and never visits c or d. Avoid relying on side effects like this in production code, but it shows that the check really stops early.
Example 3: empty arrays and nested records
$empty = [];
var_dump(array_all($empty, fn($value) => false));
$users = [
['name' => 'Ana', 'email' => 'ana@example.com'],
['name' => 'Ben', 'email' => ''],
];
var_dump(array_all($users, fn(array $user): bool => $user['email'] !== ''));bool(true) bool(false)
The empty array returns true even though the callback always returns false, because the callback is never called. The second check returns false because Ben has no email address.
Edge cases
- Empty arrays: the result is always
true. This is the usual logical rule that a statement about "all" elements of nothing holds. If an empty array should be treated as invalid, test$array !== []separately. - Keys are preserved: the callback sees the original keys, including string keys and non-sequential integer keys.
- Order: elements are visited in the array's own order, which matters if the callback has side effects or if an expensive check should run last.
- Callback type declarations: if you declare
int $valueand the array contains a string that cannot be coerced, PHP raises aTypeErrorfrom your callback. Keep type declarations consistent with the data.
Common mistakes
- Expecting
falsefor an empty array. It returnstrue. Add an explicit emptiness check when needed. - Writing a callback that returns nothing. A forgotten
returnyieldsnull, which is not a successful check. Always return a boolean explicitly. - Confusing it with
array_any().array_all()needs every element to pass, whilearray_any()needs just one. - Using side effects for logic. Because the loop stops early, a callback that collects data or counts calls will see only part of the array.
- Swapping parameters. Unlike
array_map()andarray_filter()in some forms, the array comes first and the callback second.
Related functions
array_any()checks whether at least one element satisfies a callback.array_filter()returns the elements that satisfy a callback instead of a single boolean.array_find()returns the first element that satisfies a callback.array_find_key()returns the key of the first element that satisfies a callback.
Recap
array_all() takes an array and a callback receiving the value and key, and returns true only when every element passes. It stops at the first failure, returns true for an empty array, and throws a TypeError for invalid argument types. Use it to replace hand-written validation loops with a single readable expression.
Tested examples
$scores = [82, 91, 77, 100];
var_dump(array_all($scores, fn(int $score): bool => $score >= 70));
var_dump(array_all($scores, fn(int $score): bool => $score >= 80));bool(true) bool(false)
$calls = [];
$items = ['a' => 3, 'b' => 8, 'c' => 5, 'd' => 9];
$result = array_all($items, function (int $value, string $key) use (&$calls): bool {
$calls[] = $key;
return $value < 6;
});
echo json_encode(['result' => $result, 'calls' => $calls], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
echo "\n";{"result":false,"calls":["a","b"]}
$empty = [];
var_dump(array_all($empty, fn($value) => false));
$users = [
['name' => 'Ana', 'email' => 'ana@example.com'],
['name' => 'Ben', 'email' => ''],
];
var_dump(array_all($users, fn(array $user): bool => $user['email'] !== ''));bool(true) bool(false)