PHP array_find function tutorial shows how to search array elements in PHP. Learn array_find with practical examples.
last modified March 13, 2025
The PHP array_find function searches for an element in an array using a callback function. It returns the first matching element or null.
The array_find function searches an array for the first element that satisfies a condition. It’s useful for finding specific array items.
Syntax: array_find(array $array, callable $callback): mixed. The callback should return true for matching elements. Returns null if none match.
This example demonstrates finding the first even number in an array.
basic_array_find.php
<?php
declare(strict_types=1);
function array_find(array $array, callable $callback): mixed { foreach ($array as $element) { if ($callback($element)) { return $element; } } return null; }
$numbers = [1, 3, 4, 7, 8]; $firstEven = array_find($numbers, fn($n): bool => $n % 2 === 0);
echo $firstEven ?? ‘Not found’;
The code finds the first even number (4) in the array. The callback checks each element until it finds a match, then returns it immediately.
Search for an object in an array where a specific property meets criteria.
object_property_find.php
<?php
declare(strict_types=1);
class User { public function __construct( public string $name, public int $age ) {} }
$users = [ new User(“Alice”, 25), new User(“Bob”, 30), new User(“Charlie”, 22) ];
$youngUser = array_find($users, fn(User $u): bool => $u->age < 25);
echo $youngUser?->name ?? ‘Not found’;
This finds the first user under 25 years old. The callback checks the age property, returning the matching User object (Charlie in this case).
Use array_find to locate the first string matching a regex pattern.
string_pattern_find.php
<?php
declare(strict_types=1);
$words = [“apple”, “banana”, “cherry”, “date”]; $fruitWithA = array_find($words, fn(string $w): bool => preg_match(’/a/’, $w));
echo $fruitWithA ?? ‘Not found’;
This finds the first fruit name containing the letter ‘a’. The callback uses preg_match to test each string until it finds a match (apple).
Search for items in associative arrays based on key-value combinations.
associative_array_find.php
<?php
declare(strict_types=1);
$products = [ [‘id’ => 1, ’name’ => ‘Laptop’, ‘stock’ => 5], [‘id’ => 2, ’name’ => ‘Phone’, ‘stock’ => 0], [‘id’ => 3, ’name’ => ‘Tablet’, ‘stock’ => 10] ];
$outOfStock = array_find($products, fn(array $p): bool => $p[‘stock’] === 0);
echo $outOfStock[’name’] ?? ‘All in stock’;
This locates the first product with zero stock. The callback checks the ‘stock’ key in each associative array, returning the matching item (Phone).
Combine multiple conditions in the callback for more sophisticated searches.
complex_condition_find.php
<?php
declare(strict_types=1);
$employees = [ [’name’ => ‘John’, ‘department’ => ‘IT’, ‘salary’ => 75000], [’name’ => ‘Jane’, ‘department’ => ‘HR’, ‘salary’ => 65000], [’name’ => ‘Bob’, ‘department’ => ‘IT’, ‘salary’ => 80000] ];
$highEarnerInIT = array_find($employees, fn(array $e): bool => $e[‘department’] === ‘IT’ && $e[‘salary’] > 70000 );
echo $highEarnerInIT[’name’] ?? ‘Not found’;
This finds the first IT department employee earning over 70,000. The callback combines two conditions to precisely locate the desired element (John).
Early Returns: Place likely matches early in large arrays.
Type Safety: Use type hints for robust callback functions.
Readability: Extract complex conditions to named functions.
Null Safety: Always check the return value for null.
Performance: Consider array structure for optimal searches.
PHP Array Filter Documentation (related functionality)
This tutorial covered the PHP array_find pattern with practical examples showing its usage for various array search scenarios.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP Array Functions.