PHP array_all() Function
Quick summary: The PHP array_all() function checks if all elements in an array satisfy a callback condition.
PHP array_all() Syntax
array_all(array $array, callable $callback): bool
PHP
PHP array_all() Basic examples
$numbers = [2, 4, 6];
$result = array_all($numbers, fn($n) => $n % 2 === 0);
var_dump($result);
PHP
Output:
bool(true)
Returns true if all elements match the condition.
$numbers = [2, 3, 6];
$result = array_all($numbers, fn($n) => $n % 2 === 0);
var_dump($result);
PHP
Output:
bool(false)
Returns false if at least one element fails the condition.
PHP array_all() Real-world usage
$users = [['active'=>true],['active'=>true]];
$allActive = array_all($users, fn($u) => $u['active']);
var_dump($allActive);
PHP
Output:
bool(true)
Check if all users are active.
PHP array_all() Edge cases
$empty = [];
var_dump(array_all($empty, fn($v) => false));
PHP
Output:
bool(true)
Returns true for empty arrays (vacuous truth).
PHP array_all() Common mistakes
Expecting it to modify the array
array_all() only checks condition and returns boolean.
Incorrect
array_all($arr, fn($v) => $v > 0);
Correct
$result = array_all($arr, fn($v) => $v > 0);
Store the returned boolean value.
PHP array_all() Frequently Asked Questions
What does array_all() do?
Checks if all elements satisfy a condition.
Available in PHP?
Not native before PHP 8.4.
Use case?
Validation checks.
Alternative?
array_filter().
Return type?
Boolean.
Performance?
Depends on implementation.
Common mistake?
Assuming built-in support.
Safe?
Yes.
Callback required?
Yes.
Short-circuit?
Yes.
Used in validation?
Yes.
Best practice?
Use closures.