Content · Programming · PHP

PHP array_all() Function: Usage and Examples

PHP array_all() Function Explained with Examples

4 views Published Oct 16, 2026 Updated Oct 16, 2026 Admin

Sometimes you need to check whether every element in a PHP array meets a particular condition. For example, you might want to verify that all products have valid prices or that every order item has a positive quantity.

PHP provides the array_all() function to perform these checks without writing a separate loop.

In this guide, you'll learn how array_all() works, how to use callbacks, and how to validate array data with practical examples.

PHP version requirement: The array_all() function was introduced in PHP 8.4. It is not available in earlier PHP versions.

What Is array_all() in PHP?


The array_all() function checks whether every element in an array satisfies a given condition.

Its syntax is:

array_all(array $array, callable $callback): bool

The function accepts two arguments:

  • $array — The array containing the elements to check.
  • $callback — A function that tests each element and returns a boolean result.

array_all() returns true when every element satisfies the condition. If any element fails, it returns false.

Example 1: Checking Whether All Numbers Are Positive


Let's check whether all numbers in an array are greater than zero.

<?php

$numbers = [5, 12, 8, 20];

$result = array_all(
    $numbers,
    fn(int $number): bool => $number > 0
);

var_dump($result);

Output:

bool(true)

The callback checks each number using $number > 0.

Because every number is positive, the result is true.

If you change one of the numbers to -3, the function returns false.

The fn() syntax creates an arrow function, which is a concise way to write a simple callback.

Example 2: Checking Associative Array Keys and Values


The callback can receive both the value and the key of each array element.

For example:

<?php

$settings = [
    "maxUsers" => 50,
    "timeout" => 30,
    "retryLimit" => 5
];

$result = array_all(
    $settings,
    fn($value, $key): bool =>
        is_string($key) &&
        is_int($value) &&
        $value > 0
);

var_dump($result);

Output:

bool(true)

The callback checks three conditions:

  • The array key must be a string.
  • The value must be an integer.
  • The integer must be greater than zero.

If any element fails one of these checks, array_all() returns false.

How Does array_all() Handle Empty Arrays?


An important behavior to understand is that array_all() returns true for an empty array.

<?php

$numbers = [];

$result = array_all(
    $numbers,
    fn($value): bool => $value > 0
);

var_dump($result);

Output:

bool(true)

This happens because an empty array contains no elements that violate the condition. The callback is never called.

If your application requires at least one element, check that the array isn't empty:

$valid = $numbers !== []
    && array_all(
        $numbers,
        fn($value): bool => $value > 0
    );

Stopping at the First Failed Condition


The array_all() function uses short-circuit evaluation.

As soon as the callback returns false, PHP stops checking additional elements.

For example:

<?php

$numbers = [10, 20, -5, 40];

$result = array_all(
    $numbers,
    function (int $number): bool {

        echo "Checking: $number" . PHP_EOL;

        return $number > 0;
    }
);

var_dump($result);

Output:

Checking: 10
Checking: 20
Checking: -5
bool(false)

PHP never checks 40 because the previous element already failed.

This avoids unnecessary checks and can improve efficiency when working with larger arrays.

Practical Example: Validating Order Items


Imagine an online store where every order item must have a product name and a quantity greater than zero.

We can use array_all() to validate the complete order before processing it.

Create a file named validate-order.php:

<?php

declare(strict_types=1);

// Order items.

$items = [
    ["name" => "Keyboard", "quantity" => 2],
    ["name" => "Mouse", "quantity" => 1],
    ["name" => "Monitor", "quantity" => 3]
];

// Validate all items.

$valid = $items !== [] && array_all(
    $items,
    function (mixed $item): bool {

        if (!is_array($item)) {
            return false;
        }

        $name = $item["name"] ?? null;
        $quantity = $item["quantity"] ?? null;

        return is_string($name)
            && trim($name) !== ""
            && is_int($quantity)
            && $quantity > 0;
    }
);

if ($valid) {
    echo "All order items are valid.";
} else {
    echo "The order contains invalid items.";
}

Output:

All order items are valid.

The callback checks that each item is an array with a non-empty product name and a positive integer quantity.

Try changing one quantity to 0. The validation will fail, and PHP will display:

The order contains invalid items.

This approach keeps validation logic together and avoids manually tracking whether every item passed.

array_all() vs array_any() vs array_filter()


PHP provides several functions for checking array contents, but they serve different purposes.

Function Purpose
array_all() Checks whether every element satisfies a condition.
array_any() Checks whether at least one element satisfies a condition.
array_filter() Returns an array containing elements that pass a condition.

Use array_all() when you need a single boolean answer about the entire array.

For example, checking whether all employees completed their training is a good use case.

Using PHP Versions Earlier Than 8.4


If your application runs on PHP 8.3 or earlier, you can perform the same check using foreach.

<?php

$numbers = [5, 10, 15];

$allPositive = true;

foreach ($numbers as $number) {

    if ($number <= 0) {
        $allPositive = false;
        break;
    }
}

var_dump($allPositive);

Output:

bool(true)

Like array_all(), this loop stops at the first failed condition and returns a true result for an empty array.

Key Takeaways


  • array_all() was introduced in PHP 8.4.
  • It checks whether every array element satisfies a callback condition.
  • The callback receives the element's value and, optionally, its key.
  • The function returns true only when no element fails the condition.
  • Empty arrays return true, so check for emptiness when necessary.
  • Evaluation stops immediately when an element fails.
  • Use foreach to perform equivalent checks in older PHP versions.

The array_all() function provides a clean and readable way to validate collections in PHP. It is particularly useful for checking records, verifying configuration values, and ensuring that every element meets your application's requirements.

For additional details, see the official PHP array_all() documentation.

Share this resourceSend this page to someone who may find it useful.