← All posts

PHP 8.4 array_find(), array_any() and array_all(): replace the foreach-and-break loop

PHP 8.4's array_find(), array_find_key(), array_any() and array_all() explained with tested examples, plus the null and empty-array gotchas.

Ian Croasdell at a laptop with his tabby cat Simba sitting beside the keyboard

Every PHP codebase has this loop somewhere. Set a variable to null, foreach over the array, if something matches, assign it and break. Six lines to answer "which one is it?".

PHP 8.4 added four small functions that do that job in one line each: array_find(), array_find_key(), array_any() and array_all(). They're listed in the PHP 8.4 release notes and came from the array_find RFC.

The four functions in one example

Here's a small list of jobs, keyed by job number:

```php <?php declare(strict_types=1);

$jobs = [ 'J101' => ['task' => 'Bleed radiators', 'hours' => 1.0, 'paid' => true], 'J102' => ['task' => 'Change lock cylinder', 'hours' => 0.5, 'paid' => false], 'J103' => ['task' => 'Hang curtain pole', 'hours' => 1.5, 'paid' => false], ];

// First unpaid job (the value) $firstUnpaid = array_find($jobs, fn(array $job) => !$job['paid']); echo $firstUnpaid['task'], PHP_EOL;

// Its key $firstUnpaidId = array_find_key($jobs, fn(array $job) => !$job['paid']); echo $firstUnpaidId, PHP_EOL;

// Is anything unpaid? Is everything under 2 hours? var_dump(array_any($jobs, fn(array $job) => !$job['paid'])); var_dump(array_all($jobs, fn(array $job) => $job['hours'] < 2));

// The callback also gets the key $fromJ103 = array_find($jobs, fn(array $job, string $id) => $id === 'J103'); echo $fromJ103['task'], PHP_EOL;

// No match gives null, so check with === $big = array_find($jobs, fn(array $job) => $job['hours'] > 5); var_dump($big); ```

Output on PHP 8.5:

``` Change lock cylinder J102 bool(true) bool(true) Hang curtain pole NULL ```

In plain words:

  • array_find() returns the first value where the callback returns true, or null if nothing matches.
  • array_find_key() returns the first key instead, or null.
  • array_any() returns true if at least one element matches.
  • array_all() returns true only if every element matches.

The callback gets the value first and the key second, so you can match on either.

Three gotchas worth knowing

```php <?php declare(strict_types=1); // Empty arrays var_dump(array_any([], fn($v) => true)); var_dump(array_all([], fn($v) => false)); // null is ambiguous when the array can hold null $values = [null, 0, 'x']; var_dump(array_find($values, fn($v) => $v === null)); var_dump(array_find_key($values, fn($v) => $v === null)); // Stops at the first match $calls = 0; array_find([1, 2, 3, 4], function (int $n) use (&$calls) { $calls++; return $n === 2; }); echo $calls, PHP_EOL; ```

Output:

``` bool(false) bool(true) NULL int(0) 2 ```

  1. Empty arrays. array_any([]) is false and array_all([]) is true, whatever the callback says. "Every job is paid" is technically true when there are no jobs. If an empty list should count as a problem, check $jobs === [] first.
  2. null means two things. If your array can contain null, a null from array_find() could be a match or no match. Use array_find_key() and compare the result with null instead, because a real key is never null.
  3. They stop early. The search stops at the first match, so the callback above ran twice, not four times. Don't rely on the callback for side effects on every element. That's what foreach or array_map() are for.

When a foreach is still better

If you need every match, not just the first, array_filter() is the right tool. And if the loop body does real work, a plain foreach is often easier to read than a long arrow function. In other words, these replace the find-and-break loop, not every loop.

They need PHP 8.4 or newer. If you're not sure what your site runs, check the supported versions page and my post on PHP 8.2 reaching end of life. If you've already met array_first() and array_last() in PHP 8.5, these sit nicely alongside them.

Want more like this?

Clean, typed PHP like this is what my book PHP Done The Correct Way is about, with a free companion code page. If you'd like help tidying up a PHP project, have a look at PHP AIR, and for more tips see the PHP AIR blog.

Let's talk More posts