PHP array_all Function
last modified March 13, 2025
The PHP array_all function checks if all elements in an array
pass a test implemented by a callback function. It's useful for validation.
Basic Definition
The array_all function tests whether all elements satisfy a
condition. It returns true if all elements pass the test, false otherwise.
Syntax: array_all(array $array, callable $callback): bool. The
callback should return true for passing elements. Empty arrays return true.
Basic array_all Example
This shows simple validation of all array elements being positive numbers.
<?php
declare(strict_types=1);
function array_all(array $array, callable $callback): bool {
foreach ($array as $element) {
if (!$callback($element)) {
return false;
}
}
return true;
}
$numbers = [2, 4, 6, 8];
$allEven = array_all($numbers, fn($n): bool => $n % 2 === 0);
echo $allEven ? 'All even' : 'Not all even';
This checks if all numbers are even. The callback tests each element, and
array_all returns true since all elements pass the test.
Validating String Lengths
Check if all strings in an array meet a minimum length requirement.
<?php declare(strict_types=1); $names = ["Alice", "Bob", "Charlie"]; $allLongEnough = array_all($names, fn($name): bool => strlen($name) >= 3); echo $allLongEnough ? 'All valid' : 'Some too short';
This verifies all names have at least 3 characters. The callback checks string length, returning true for all elements in this case.
Object Property Validation
Validate that all objects in an array have a property meeting criteria.
<?php
declare(strict_types=1);
class Product {
public function __construct(
public string $name,
public float $price
) {}
}
$products = [
new Product("Laptop", 999.99),
new Product("Phone", 699.99),
new Product("Tablet", 399.99)
];
$allExpensive = array_all($products, fn(Product $p): bool => $p->price > 300);
echo $allExpensive ? 'All expensive' : 'Some cheap';
This checks if all products cost more than 300. The callback examines each object's price property, returning true for this dataset.
Empty Array Behavior
array_all returns true for empty arrays, which can be useful.
<?php declare(strict_types=1); $emptyArray = []; $result = array_all($emptyArray, fn($x): bool => $x > 10); echo $result ? 'All pass (vacuous truth)' : 'Some fail';
With no elements to check, array_all returns true. This follows
mathematical logic where universal quantification over an empty set is true.
Early Termination
array_all stops checking after first failure for efficiency.
<?php
declare(strict_types=1);
$numbers = [2, 4, 5, 8];
$allEven = array_all($numbers, function($n): bool {
echo "Checking $n\n";
return $n % 2 === 0;
});
echo $allEven ? 'All even' : 'Not all even';
The function stops at the first odd number (5). You'll only see output for 2, 4, and 5, demonstrating the short-circuit behavior.
Best Practices
- Clear Callbacks: Use descriptive names for callback logic.
- Type Safety: Add type hints for robust validation.
- Performance: Place likely failures early in large arrays.
- Readability: Consider helper functions for complex checks.
Source
PHP Array Filter Documentation (related functionality)
This tutorial covered the PHP array_all pattern with practical
examples showing its usage for array validation scenarios.
Author
List all PHP Array Functions.