PHP array_all Function: How it Works with Examples

php array_all

PHP 8.4 released a new built-in function, array_all checks to check if all array values meet a condition. It gives a true or false result.

Understand the array_all Function in PHP

The array_all checks all values with one callback function and returns true only if every value passes the rule.

The syntax looks like this:

array_all(array $array, callable $callback)

Here is how it works:

  • array → input array you want to check.
  • callback → function that tests each value.
  • return → true if all values pass. False if one value fails.

The array_all runs the callback on each array value. If one test fails, the function stops and returns false. If all pass, the result is true.

Use array_all to confirm that an array has only valid data. For example, you can test if all numbers are positive or if all strings match a pattern.

Here is a quick example:

$result = array_all([2, 4, 6], fn($n) => $n % 2 === 0);
var_dump($result);

This code checks if all numbers are even and gives true because every number divides by 2.

Examples of the array_all in PHP

Check if All Numbers are Positive:

$result = array_all([1, 3, 5], fn($n) => $n > 0);
var_dump($result);

This code checks if every number is positive. The function runs through the array and returns true since all numbers pass.

Confirm All Values are Strings:

$result = array_all(["a", "b", "c"], fn($v) => is_string($v));
var_dump($result);

This checks if every array value is a string. The callback tests each element and returns true because all are valid strings.

Check Mixed Values for Even Numbers:

$result = array_all([2, 4, 7, 8], fn($n) => $n % 2 === 0);
var_dump($result);

This array has one odd number. The callback fails on 7, so the function returns false.

Confirm Email Format with Regex:

$result = array_all(
  ["a@mail.com", "b@mail.com"],
  fn($v) => filter_var($v, FILTER_VALIDATE_EMAIL) !== false
);
var_dump($result);

The code checks if all values match a valid email format. Since both values pass, the result comes back as true.

Wrapping Up

You learned how array_all checks all values with a rule and how it compares to array_filter.

Here is a quick recap:

  • array_all gives true if all values pass one rule.
  • array_filter gives a new array with only valid values.
  • Use array_all for boolean checks.
  • Use array_filter to build a valid array.

FAQs

What is PHP array_all and how does it work?

array_all is not a built-in PHP function (until PHP 8.4 introduces it). It is a helper function that checks if all elements in an array satisfy a specific condition. The condition is passed as a callback function.

Here is how to build the helper function for PHP versions earlier than 8.4:


if( ! function_exists('array_all') ) {
function array_all($array, $callback) {
    foreach ($array as $value) {
        if (!$callback($value)) {
            return false;
        }
    }
    return true;
}
}
$result = array_all([2, 4, 6], function($n) {
    return $n % 2 === 0;
});

echo $result ? "All even" : "Not all even";

How to use PHP array_all with associative arrays?

PHP array_all works with associative arrays by checking values or keys.

$data = ["a" => 5, "b" => 10, "c" => 15];

$result = array_all($data, function($value) {
    return $value > 3;
});

echo $result ? "All greater than 3" : "Some not greater";

What is the difference between PHP array_all and array_filter?

array_all returns true if all elements match. array_filter returns filtered elements that match.

$nums = [2, 4, 6, 8];

$all = array_all($nums, function($n) {
    return $n % 2 === 0;
});

$filtered = array_filter($nums, function($n) {
    return $n % 2 === 0;
});

echo $all ? "All passed" : "Some failed";
print_r($filtered);

Similar Reads

PHP XML Parsers: Everything You Need to Know

In PHP, manipulation of XML data can be very critical when most systems' data exchange relies on the format. XML—Extensible…

PHP array_find: How to Locate Array Values with Examples

PHP array_find was released in PHP 8.4 to locate a value inside an array and returns the first match. Understand…

PHP NOT ( ! ) Operator: A Comprehensive Guide

The NOT operator in PHP (!) provides the reverse of whatever truth value is given by a certain expression. It’s…

PHP array_diff_ukey Function: How it Works with Examples

The PHP array_diff_ukey function compares keys in two or more arrays. It returns the keys from the first array that…

PHP array_is_list: Check if an Array is a List with Examples

You need the array_is_list function to know if an array works as a list or not in PHP. What is…

PHP array_diff_uassoc: How to Compare Arrays with Keys

PHP array_diff_uassoc compares arrays by values and keys. You can also pass a custom function to compare keys in your…

PHP array_key_first Function: How it Works with Examples

The array_key_first helps you to get the first key of an array directly in PHP. It saves time and removes…

PHP MySQL LIMIT Data: How to Optimize PHP Queries?

There are situations where you only need a specific number of rows returned. This is where the "LIMIT" clause in…

How to Insert Data into MySQL with PHP

pplications. Such As as From registering new users to collecting form submissions and storing product details. Things like adding a…

Understanding the PHP Enumerable

Actually, the PHP enumerable is a new feature of 8.1 which allows you to define a new type. This feature…

Previous Article

HTML Deprecated Tags and Modern Alternatives List

Next Article

Arrow Function in JavaScript: How it Works with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.