{
    "slug": "array_functions",
    "term": "Key Array Functions (array_map, array_filter, array_reduce…)",
    "category": "php",
    "difficulty": "beginner",
    "short": "PHP's functional array processing functions enable expressive, pipeline-style transformations without explicit loops.",
    "long": "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.",
    "aliases": [
        "PHP array functions",
        "array_map",
        "array_filter",
        "array manipulation"
    ],
    "tags": [
        "php",
        "arrays",
        "functional"
    ],
    "misconception": "array_map and array_filter always preserve keys. array_map on a single array preserves keys; array_filter always preserves keys (use array_values() to re-index). Knowing the key behaviour of each function prevents subtle indexing bugs.",
    "why_it_matters": "PHP's built-in array functions are implemented in C and outperform equivalent manual loops — using them correctly also makes code more declarative and less error-prone.",
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "closures",
        "arrow_functions",
        "generators"
    ],
    "prerequisites": [
        "php_data_types",
        "big_o_notation",
        "generators"
    ],
    "refs": [
        "https://www.php.net/manual/en/ref.array.php"
    ],
    "bad_code": "// Manual loop instead of built-in function:\n$names = [];\nforeach ($users as $user) {\n    $names[] = $user['name']; // Use array_column($users, 'name')\n}\n\n// Unsafe search:\n$pos = array_search($val, $arr);\nif ($pos == false) { /* misses index 0 — use !== false */ }",
    "good_code": "// array_map — transform\n$prices  = array_map(fn(Product $p) => $p->price * 1.2, $products);\n\n// array_filter — keep truthy or matching\n$active  = array_filter($users, fn(User $u) => $u->isActive());\n// re-index after filter\n$active  = array_values($active);\n\n// array_column — extract column, optionally index by another\n$names   = array_column($users, 'name');           // ['Alice','Bob']\n$byId    = array_column($users, 'name', 'id');     // [1=>'Alice',2=>'Bob']\n\n// usort — custom sort with spaceship operator\nusort($orders, fn($a, $b) => $a->total <=> $b->total);\n\n// array_combine, array_diff, array_intersect\n$map     = array_combine($keys, $values);\n$missing = array_diff($required, $provided);",
    "quick_fix": "Prefer array_map/array_filter/array_reduce over loops for clarity; use array_column for extracting key columns from 2D arrays; use array_flip for O(1) lookups",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/array_functions",
        "html_url": "https://codeclaritylab.com/glossary/array_functions",
        "json_url": "https://codeclaritylab.com/glossary/array_functions.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Key Array Functions (array_map, array_filter, array_reduce…)](https://codeclaritylab.com/glossary/array_functions) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/array_functions"
            }
        }
    }
}