Arrays7 min readExamples run on PHP 8.5.10

array_all()

Learn how PHP 8.4 array_all() checks whether every array element passes a callback, with examples, edge cases and common mistakes.

Signature

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

Purpose

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 return true when the element is acceptable and false when 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:

  • true if the callback returns true for every element.
  • false as soon as the callback returns false for 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.

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

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

Example
<?php
$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'] !== ''));
Output
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 $value and the array contains a string that cannot be coerced, PHP raises a TypeError from your callback. Keep type declarations consistent with the data.

Common mistakes

  1. Expecting false for an empty array. It returns true. Add an explicit emptiness check when needed.
  2. Writing a callback that returns nothing. A forgotten return yields null, which is not a successful check. Always return a boolean explicitly.
  3. Confusing it with array_any(). array_all() needs every element to pass, while array_any() needs just one.
  4. 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.
  5. Swapping parameters. Unlike array_map() and array_filter() in some forms, the array comes first and the callback second.
  • 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

Checking a rule on every value
<?php
$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));
Output
bool(true)
bool(false)
Using keys and observing short-circuiting
<?php
$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";
Output
{"result":false,"calls":["a","b"]}
Empty arrays and nested records
<?php
$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'] !== ''));
Output
bool(true)
bool(false)