PHP array_any Function
last modified March 13, 2025
The PHP array_any
function checks if any element in an array
passes a test implemented by a callback function. It's useful for partial
validation.
Basic Definition
The array_any
function tests whether any element satisfies a
condition. It returns true if at least one element passes the test.
Syntax: array_any(array $array, callable $callback): bool
. The
callback should return true for passing elements. Empty arrays return false.
Basic array_any Example
This shows simple validation checking if any array element is positive.
<?php declare(strict_types=1); function array_any(array $array, callable $callback): bool { foreach ($array as $element) { if ($callback($element)) { return true; } } return false; } $numbers = [-2, -1, 0, 1]; $hasPositive = array_any($numbers, fn($n): bool => $n > 0); echo $hasPositive ? 'Has positive' : 'No positives';
This checks if any number is positive. The callback tests each element, and
array_any
returns true since 1 passes the test.
Checking for Specific Values
Verify if any string in an array matches a specific value.
<?php declare(strict_types=1); $colors = ["red", "green", "blue"]; $hasBlue = array_any($colors, fn($color): bool => $color === "blue"); echo $hasBlue ? 'Has blue' : 'No blue';
This checks if "blue" exists in the array. The callback compares each element, returning true when it finds the matching value.
Object Property Check
Check if any object in an array has a property meeting certain criteria.
<?php declare(strict_types=1); class User { public function __construct( public string $name, public bool $isAdmin ) {} } $users = [ new User("Alice", false), new User("Bob", false), new User("Charlie", true) ]; $hasAdmin = array_any($users, fn(User $u): bool => $u->isAdmin); echo $hasAdmin ? 'Has admin' : 'No admins';
This verifies if any user is an admin. The callback checks each object's isAdmin property, returning true when it finds an admin user.
Empty Array Behavior
array_any
returns false for empty arrays, which is logical.
<?php declare(strict_types=1); $emptyArray = []; $result = array_any($emptyArray, fn($x): bool => $x > 10); echo $result ? 'Some pass' : 'None pass';
With no elements to check, array_any
returns false. This follows
mathematical logic where existential quantification over an empty set is false.
Early Termination
array_any
stops checking after first success for efficiency.
<?php declare(strict_types=1); $numbers = [1, 3, 5, 8, 9]; $hasEven = array_any($numbers, function($n): bool { echo "Checking $n\n"; return $n % 2 === 0; }); echo $hasEven ? 'Has even' : 'No evens';
The function stops at the first even number (8). You'll only see output for 1, 3, 5, and 8, 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 matches early in large arrays.
- Readability: Consider helper functions for complex checks.
Source
PHP Array Filter Documentation (related functionality)
This tutorial covered the PHP array_any
pattern with practical
examples showing its usage for array validation scenarios.
Author
List all PHP Array Functions.