Key Array Functions (array_map, array_filter, array_reduce…)
debt(d4/e2/b2/t5)
Closest to 'default linter catches the common case' (d3), +1. phpcs, phpstan, and rector (listed in detection_hints.tools) can catch some misuses like in_array in loops or manual loops replaceable by array_map/filter. However, subtler issues like key-preservation surprises or array_search loose comparison aren't reliably caught by default lint rules — they often require more targeted static analysis rules or code review. Scoring d4 as slightly harder than default linting but not requiring a fully specialist tool.
Closest to 'one-line patch or single-call swap' (e1), +1. The quick_fix describes straightforward replacements: swap a foreach for array_map, add array_values() to re-index, replace in_array loop with array_flip+isset. Most fixes are one-to-few-line changes within a single function. Slightly above e1 because some cases (e.g., replacing nested loops with array_column or refactoring a usort callback) require understanding the data flow, but still very localized.
Closest to 'minimal commitment' (b1), +1. Array functions are utility-level choices that apply broadly (web, cli, queue-worker across all PHP 5+), but each usage is localized and independent. Choosing array_map over a foreach doesn't impose structural weight on the rest of the codebase. The reach is wide but the commitment per instance is minimal — no architectural coupling or persistent tax.
Closest to 'notable trap' (t5). The misconception field describes a documented gotcha that most PHP devs eventually learn: array_map preserves keys on single arrays, array_filter always preserves keys (requiring array_values to re-index), and array_search returns 0 for the first element which is loosely equal to false. These are genuine traps — the 'obvious' assumption about key behavior or return values is wrong — but they're well-documented and commonly known. The usort callback true/false vs negative/zero/positive is another classic gotcha. Collectively these represent notable but not catastrophic traps.
Also Known As
TL;DR
Explanation
PHP provides rich array manipulation: array_map() transforms each element, array_filter() selects elements matching a predicate, array_reduce() aggregates to a single value, usort()/uasort()/uksort() sort with custom comparators, array_column() extracts a column from a multi-dimensional array, and array_combine() pairs keys and values from two arrays. Understanding null handling (array_map with null callback), reference passing pitfalls in foreach, and the difference between array_splice() and array_slice() prevents subtle bugs.
Common Misconception
Why It Matters
Common Mistakes
- Using a foreach loop to build a new array when array_map() expresses the same transformation more clearly.
- Using array_search() and assuming false means not found — it returns 0 for the first element, which is loosely equal to false.
- Not using array_column() for extracting a column from a multi-dimensional array — a common manual loop anti-pattern.
- Passing the wrong argument order to usort() callback — return negative/zero/positive, not true/false.
Code Examples
// Manual loop instead of built-in function:
$names = [];
foreach ($users as $user) {
$names[] = $user['name']; // Use array_column($users, 'name')
}
// Unsafe search:
$pos = array_search($val, $arr);
if ($pos == false) { /* misses index 0 — use !== false */ }
// array_map — transform
$prices = array_map(fn(Product $p) => $p->price * 1.2, $products);
// array_filter — keep truthy or matching
$active = array_filter($users, fn(User $u) => $u->isActive());
// re-index after filter
$active = array_values($active);
// array_column — extract column, optionally index by another
$names = array_column($users, 'name'); // ['Alice','Bob']
$byId = array_column($users, 'name', 'id'); // [1=>'Alice',2=>'Bob']
// usort — custom sort with spaceship operator
usort($orders, fn($a, $b) => $a->total <=> $b->total);
// array_combine, array_diff, array_intersect
$map = array_combine($keys, $values);
$missing = array_diff($required, $provided);