← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

array_find() / array_find_key() (PHP 8.4)

php PHP 8.4+ Beginner

Also Known As

array_find array_find_key array_any array_all PHP 8.4 arrays

TL;DR

PHP 8.4 adds array_find() and array_find_key() — built-in functions that return the first element (or key) matching a callback predicate, replacing verbose foreach loops or array_filter() + reset() patterns.

Explanation

Finding the first matching element in an array was always awkward in PHP — either a foreach with a break, or array_filter() which returns all matches followed by reset() or current() to grab the first. PHP 8.4 adds four related functions: array_find() returns the first value where the callback returns true, array_find_key() returns its key, array_any() returns true if any element matches, and array_all() returns true if all elements match. These mirror JavaScript's Array.find(), Array.findIndex(), Array.some(), and Array.every(). All four short-circuit — they stop iterating as soon as the result is determined.

Common Misconception

array_find() returns an array of matches like array_filter(). It returns only the first matching value (or null if none match). For all matches, use array_filter().

Why It Matters

These functions make intent clearer and eliminate boilerplate. A foreach-with-break for 'find first matching' is four to six lines of code with a mutable variable; array_find() expresses the same intent in one line. array_any() and array_all() replace common patterns like 'count(array_filter(...)) > 0' with readable, short-circuiting alternatives.

Common Mistakes

  • Expecting array_find() to return false on no match like reset() — it returns null, so check with '=== null' not '=== false'.
  • Using array_find() when you need all matches — use array_filter() for multiple results.
  • Forgetting array_any() and array_all() exist — 'count(array_filter(...)) > 0' is a common pattern these replace cleanly.
  • Using these functions in PHP < 8.4 — they are not available; polyfill with a helper function or upgrade.

Code Examples

✗ Vulnerable
<?php
// ❌ Verbose first-match patterns
$firstAdmin = null;
foreach ($users as $user) {
    if ($user['role'] === 'admin') {
        $firstAdmin = $user;
        break;
    }
}

// Or the filter+reset hack
$admins = array_filter($users, fn($u) => $u['role'] === 'admin');
$firstAdmin = reset($admins); // false if empty, not null
✓ Fixed
<?php
// ✅ PHP 8.4 — clean, intent-revealing
$firstAdmin = array_find($users, fn($u) => $u['role'] === 'admin');
// Returns null if not found (not false)

$adminKey = array_find_key($users, fn($u) => $u['role'] === 'admin');

$hasAdmin  = array_any($users, fn($u) => $u['role'] === 'admin');
$allActive = array_all($users, fn($u) => $u['active'] === true);

// Null-safe chaining when element may not exist
$email = array_find($users, fn($u) => $u['id'] === 42)?->email;

Added 23 Mar 2026
Views 24
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 8 Perplexity 5 Google 2 ChatGPT 1 Ahrefs 1
crawler 16 crawler_json 1
DEV INTEL Tools & Severity
⚙ Fix effort: Low
⚡ Quick Fix
Replace 'foreach ($arr as $v) { if ($cond) { $found = $v; break; } }' with '$found = array_find($arr, fn($v) => $cond);'
📦 Applies To
PHP 8.4+ web cli

✓ schema.org compliant