Backend Development

PHP 8’s Complete Guide to array_find() Family functions

PHP 8’s array_find() Family: A Complete Guide to array_find(), array_find_key(), array_any(), array_all(), and More

For years, PHP developers have relied on combinations of foreach, array_filter(), array_search(), and custom helper functions to locate elements inside arrays.
While these approaches work, they often introduce unnecessary complexity, extra memory allocations, or verbose code.

 

 

 

Recent versions of PHP introduce a family of array helper functions that make searching and testing arrays significantly more expressive:

  •  array_find()
  •  array_find_key()
  •  array_any() 
  • array_all() 
  • array_find_keys()

These functions embrace a functional programming style while remaining easy to understand for everyday PHP development. This article explores each function in depth, explains how they differ from existing array functions, discusses performance considerations, and provides practical examples.

 

Why Were These Functions Added?

Consider a common task: finding the first active user.

Traditionally, you’d write:

$user = null;

foreach ($users as $candidate) {
    if ($candidate['active']) {
        $user = $candidate;
        break;
    }
}

This works, but it requires:

  • creating a temporary variable

  • manually breaking the loop

  • several lines of boilerplate

This can be improved using array_filter:

$active = array_filter(
    $users,
    fn ($user) => $user['active']
);

$user = reset($active);

Unfortunately, this isn’t ideal either.

array_filter() evaluates every element, even though we only need the first match.

The new array helper functions solve this elegantly.


 

array_find()

array_find() returns the first value that satisfies a predicate.

Syntax

array_find(array $array, callable $callback): mixed

The callback receives:

function ($value, $key): bool

If no matching element exists, the function returns null.

Example:

$numbers = [3, 8, 11, 18];

$result = array_find(
    $numbers,
    fn ($value) => $value > 10
);

var_dump($result);

Output: 11

Notice that  18 is never examined after 11 matches.

Example: Finding an Object

$product = array_find(
    $products,
    fn ($product) => $product->id === 42
);

Instead of writing an entire loop, the intent becomes immediately obvious.

Example: Searching Assoctiative Arrays

$config = [
    'host' => 'localhost',
    'port' => 3306,
    'ssl' => true,
];

$value = array_find(
    $config,
    fn ($value) => is_bool($value)
);

Output: true

 

Example: Accessing the key

The callback receives both value and key.

$result = array_find(
    $users,
    fn ($user, $id) => $id === 25
);

This makes searching associative arrays straightforward.

 

array_find_key()

array_find_key() returns the first matched key instead of the value that satisfies a predicate.

$key = array_find_key(
    $users,
    fn ($user) => $user['email'] === $email
);

This avoids an additional lookup. Without it, you’d typically write:

foreach ($users as $key => $user) {
    if ($user['email'] === $email) {
        break;
    }
}

 

array_find_keys()

In some occasions you want every matching key.

Example: Finding all user Id’s whose admin:

$users = [
    15 => ['admin' => true],
    28 => ['admin' => false],
    44 => ['admin' => true],
];
$keys = array_find_keys(
    $users,
    fn ($user) => $user['admin']
);

Output:

[
    15,
    44,
]

Unlike array_find_key() this function continues iterating until the end of the array.

 

array_any()

Sometimes you’re only interested in whether any element satisfies a condition.

Example:

$hasNegative = array_any(
    $numbers,
    fn ($number) => $number < 0
);

Output: false

It works much like Javascript Array.some().This replaces common patterns like:

$found = false;

foreach ($numbers as $number) {
    if ($number < 0) {
        $found = true;
        break;
    }
}

Or

count(array_filter(...)) > 0

 

array_all()

The opposite of array_any(). Returns true only if every element satisfies the condition.

$numbers = [1, 5, 10];

$result = array_all(
    $numbers,
    fn ($number) => $number > 0
);

Output: true

Example: array_all() very useful in validation.

$isValid = array_all(
    $emails,
    fn ($email) => filter_var($email, FILTER_VALIDATE_EMAIL)
);

 

Early Exit Behavior

One major advantage for the new array functions over array_filter is early termination.

Consider:

$result = array_find(
    $numbers,
    fn ($value) => $value > 10
);

Iteration stops immediately after the first match.

Similarly:

array_any(...)

stops after the first true.

array_all(...)

stops after the first false.

This can significantly reduce work on large arrays.

 

Practical Examples

Example: Finding the First Premium Customer

$customer = array_find(
    $customers,
    fn ($customer) => $customer->premium
);

Example: Find the User ID

$userId = array_find_key(
    $users,
    fn ($user) => $user['username'] === 'alice'
);

Example: Check whether an Admin Exists

adminExists = array_any(
    $users,
    fn ($user) => $user['role'] === 'admin'
);

Example: Verify All Files Exist

$allExist = array_all(
    $files,
    fn ($file) => file_exists($file)
);

 

Handling null values

array_find() returns null when nothing matches. If null is also a legitimate value in your array, you cannot distinguish between:

[
    null,
]

and 

[]

using the return value alone. In such cases, consider using array_find_key() first to determine whether a match exists before retrieving the corresponding value.

0 0 votes
Article Rating

What's your reaction?

Excited
0
Happy
0
Not Sure
0
Confused
0

You may also like

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted