ZetCode

PHP array_find Function

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.

Basic Definition

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.

Basic array_find Example

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.

Finding Objects by Property

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).

Finding Strings by Pattern

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).

Finding in Associative Arrays

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).

Finding with Complex Conditions

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).

Best Practices

Source

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.

Author

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.