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

array_find() / array_find_key() (PHP 8.4)

PHP PHP 8.4+ Beginner
debt(d7/e1/b1/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints.tools field is unspecified. The main misuse — expecting multiple results from array_find() or checking === false instead of === null — produces no error, just a silent logical bug returning null where the developer expects false or a collection. No standard linter rule targets this pattern; it requires code review or test-driven discovery. Slightly better than d9 because null-vs-false bugs do surface when the value is actually used downstream.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix is explicit: replace a foreach-with-break pattern with a single array_find() call, or swap the null check from === false to === null. Both corrections are single-line or near-single-line changes with no cross-component impact.

b1 Burden Structural debt — long-term weight of choosing wrong

Closest to 'minimal commitment' (b1). array_find() is a single utility call applied at the call site. It does not impose structural obligations on the rest of the codebase, carries no persistent architectural weight, and is easily swapped out for array_filter() or a foreach if needed. The applies_to scope (web, cli) is broad but the function itself is localised at call sites.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception is explicit: developers familiar with array_filter() expect array_find() to return all matches (an array), or expect false on no-match like reset(), but it returns only the first match or null. This is a documented gotcha that PHP developers coming from array_filter() usage will encounter, but it is a single well-defined surprise rather than a systemic contradiction.

About DEBT scoring →

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 57
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 3 pings T 2 pings F 0 pings S 2 pings S 1 ping M 2 pings T 2 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Scrapy 11 Amazonbot 10 Perplexity 5 ChatGPT 4 Google 4 Ahrefs 3 SEMrush 3 Claude 2 Bing 1 PetalBot 1
crawler 41 crawler_json 3
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