Check Array Values using array_any and array_all in PHP 8.4

Check Array Values using array_any and array_all in PHP 8.4

Since PHP 8.4, the array_any and array_all functions can be used to evaluate array elements based on a custom condition. These functions offer a more concise and readable approach to checking array conditions, eliminating the need for custom loops and improving code clarity.

  • The array_any function returns true if at least one element in the array satisfies the callback condition.
  • The array_all function returns true only if all elements in the array satisfy the callback condition.
<?php

echo (int) array_any([-2, -1, 0, 1, 2], fn (int $value) => $value > 0); // true
echo (int) array_all([-2, -1, 0, 1, 2], fn (int $value) => $value > 0); // false

Leave a Comment

Cancel reply

Your email address will not be published.