{
    "slug": "php_array_functions_fp",
    "term": "array_map / filter / reduce as FP Patterns",
    "category": "php",
    "difficulty": "intermediate",
    "short": "PHP's array_map(), array_filter(), and array_reduce() enable functional-style data transformation pipelines — cleaner than imperative loops for many common patterns.",
    "long": "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.",
    "aliases": [],
    "tags": [
        "php",
        "functional",
        "arrays",
        "fp"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "array_functions",
        "arrow_functions",
        "generators",
        "closures"
    ],
    "prerequisites": [
        "array_functions",
        "arrow_functions"
    ],
    "refs": [
        "https://www.php.net/manual/en/function.array-map.php"
    ],
    "bad_code": "$result = [];\nforeach ($users as $user) {\n    if ($user->isActive()) {\n        $result[] = $user->getName();\n    }\n}",
    "good_code": "$result = array_map(\n    fn(User $u) => $u->getName(),\n    array_filter($users, fn(User $u) => $u->isActive())\n);\n\n// Sum with reduce:\n$total = array_reduce($orders, fn($carry, $o) => $carry + $o->amount, 0);",
    "quick_fix": "Replace imperative loops with array_map/filter/reduce for simple transformations. Add array_values() after array_filter() when sequential keys are needed.",
    "severity": "info",
    "effort": "low",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_array_functions_fp",
        "html_url": "https://codeclaritylab.com/glossary/php_array_functions_fp",
        "json_url": "https://codeclaritylab.com/glossary/php_array_functions_fp.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": "[array_map / filter / reduce as FP Patterns](https://codeclaritylab.com/glossary/php_array_functions_fp) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_array_functions_fp"
            }
        }
    }
}