Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

array_any

Checks whether at least one element satisfies a condition.

Syntax

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

Parameters

ParameterTypeDescription
$arrayarrayThe input array.
$callbackcallableA test function: receives the value and the key.

Return value

bool`true` if the function returned `true` for at least one element.

Examples

Is any product out of stock:

<?php
 
$stock = ['tea' => 5, 'coffee' => 0, 'juice' => 12];
 
var_dump(array_any($stock, fn ($count) => $count === 0));

Output:

bool(true)

Notes

The function appeared in PHP 8.4. Iteration stops at the first matching element.

See also

  • array_all — Checks whether every element satisfies a condition.
  • array_find — Returns the first element for which the function returned true.
  • in_array — Checks whether a value exists in an array.

Related recipes

In the tutorial

  • New in PHP 8.4 — PHP 8.4 features — property hooks, asymmetric visibility, calling a method right after new, and new array functions.

Thanks for reading!

Donate to the site — we work hard for you.