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

array_map / filter / reduce as FP Patterns

PHP PHP 5.3+ Intermediate
debt(d7/e3/b3/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The key misuse — forgetting array_values() after array_filter() — produces no error or warning at runtime in most paths; it silently preserves non-sequential keys and only manifests as a bug when JSON encoding converts the array to an object instead of an array, or when downstream code assumes 0-indexed keys. The detection_hints list only Rector, which handles refactoring patterns rather than detecting this specific key-preservation gotcha automatically.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix notes adding array_values(array_filter($arr)) when sequential keys are needed — a small, localised change. It may need to be applied across multiple call sites if the pattern is repeated, but each fix is a simple wrapper addition rather than a structural refactor.

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

Closest to 'localised tax' (b3). The choice to use array_map/filter/reduce is localised to the specific transformation logic. It applies broadly across web/cli/queue contexts (per applies_to), but each usage is independent; there is no strong gravitational pull on the rest of the codebase. Nested usage or large-dataset concerns are contained to the affected code paths.

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

Closest to 'notable trap' (t5). The misconception is clearly documented: array_filter() preserves original keys, not re-indexing to 0-based. This contradicts many developers' intuition that filtering produces a clean sequential array, and it causes subtle JSON encoding bugs (object vs. array). It is a widely-known gotcha that most PHP developers encounter, placing it squarely at t5.

About DEBT scoring →

TL;DR

PHP's array_map(), array_filter(), and array_reduce() enable functional-style data transformation pipelines — cleaner than imperative loops for many common patterns.

Explanation

Functional array operations: array_map(fn, array) — transforms each element, returns new array. array_filter(array, fn) — removes elements where fn returns false, preserves keys. array_reduce(array, fn, initial) — folds array to single value. Combining: array_values(array_filter(array_map(fn, $data))). PHP 8.1 arrow functions (fn($x) => $x*2) make these concise. Caveats: array_filter() preserves keys — use array_values() to re-index. array_map() with multiple arrays fills shorter ones with null. For large datasets, generators are more memory-efficient than array_map.

Common Misconception

array_filter() re-indexes the array — it preserves original keys. Use array_values(array_filter($arr)) to get a 0-indexed result.

Why It Matters

FP-style array operations express intent more clearly than imperative loops and are easier to compose, test, and reason about.

Common Mistakes

  • Forgetting array_filter preserves keys — causes issues with JSON encoding expecting arrays.
  • Using array_map when a generator would use less memory for large datasets.
  • Nested array_map/filter — consider a single loop for readability beyond 2 levels.

Code Examples

✗ Vulnerable
$result = [];
foreach ($users as $user) {
    if ($user->isActive()) {
        $result[] = $user->getName();
    }
}
✓ Fixed
$result = array_map(
    fn(User $u) => $u->getName(),
    array_filter($users, fn(User $u) => $u->isActive())
);

// Sum with reduce:
$total = array_reduce($orders, fn($carry, $o) => $carry + $o->amount, 0);

Added 23 Mar 2026
Views 54
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 1 ping W 2 pings T 4 pings F 1 ping S 2 pings S 3 pings M 1 ping T 2 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Scrapy 13 Perplexity 7 Amazonbot 7 Google 4 Unknown AI 3 Ahrefs 3 SEMrush 3 Claude 2 PetalBot 2 ChatGPT 1 Meta AI 1 Bing 1 Majestic 1
crawler 43 crawler_json 4 pre-tracking 1
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Replace imperative loops with array_map/filter/reduce for simple transformations. Add array_values() after array_filter() when sequential keys are needed.
📦 Applies To
PHP 5.3+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
array_map\(|array_filter\(|array_reduce\(
Auto-detectable: ✗ No rector
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Low Context: Function


✓ schema.org compliant