{
    "slug": "array_data_structure",
    "term": "Arrays",
    "category": "data_structures",
    "difficulty": "beginner",
    "short": "The most fundamental data structure — a contiguous block of memory holding elements of the same type, offering O(1) index access but O(n) insertion and deletion in the middle.",
    "long": "True arrays (C-style) store elements contiguously in memory — index access is O(1) because element address = base + (index × element_size). PHP arrays are actually ordered hash maps, not true arrays, which is why they support mixed keys and O(1) key lookup. Dynamic arrays (ArrayList, Python list) resize by doubling capacity when full — amortised O(1) append. Understanding the difference explains why array_push() is fast but array_unshift() is O(n), and why a PHP array with integer keys 0..n behaves differently from one with gaps.",
    "aliases": [
        "dynamic array",
        "fixed array",
        "ArrayList",
        "contiguous memory"
    ],
    "tags": [
        "data-structures",
        "arrays",
        "fundamentals"
    ],
    "misconception": "PHP arrays are arrays — PHP arrays are ordered hash maps; they support integer and string keys, maintain insertion order, and have O(1) key lookup, which is very different from a C-style contiguous array.",
    "why_it_matters": "Understanding array memory layout explains why array_shift() is O(n) (reindexes everything) while array_pop() is O(1) (removes last element), and why SplFixedArray uses less memory than a PHP array for numeric data.",
    "common_mistakes": [
        "array_unshift() in a loop — O(n) reindex on every call; use a queue or prepend to a linked list.",
        "Using PHP arrays for large numeric datasets — SplFixedArray uses ~60% less memory for integer-keyed arrays.",
        "Assuming count() is O(1) — it is in PHP (cached), but in other languages it may traverse the structure.",
        "Copying large arrays by value — PHP arrays are copy-on-write, but explicit assignment in loops still causes copies."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "linked_list",
        "hash_table",
        "spl_data_structures",
        "big_o_notation"
    ],
    "prerequisites": [
        "big_o_notation",
        "hash_table",
        "php_data_types"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Array_(data_structure)"
    ],
    "bad_code": "// O(n²) — array_unshift in a loop reindexes entire array each time:\n$result = [];\nforeach ($items as $item) {\n    array_unshift($result, $item); // O(n) reindex — loop becomes O(n²)\n}",
    "good_code": "// O(n) — append then reverse once:\n$result = [];\nforeach ($items as $item) {\n    $result[] = $item; // O(1) append\n}\n$result = array_reverse($result); // O(n) once at end\n\n// Or use SplStack / SplDoublyLinkedList for O(1) prepend:\n$stack = new SplStack();\nforeach ($items as $item) $stack->push($item);",
    "quick_fix": "PHP arrays are ordered hash maps — use SplFixedArray when you need a true fixed-size numeric array with lower memory overhead for large datasets",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/array_data_structure",
        "html_url": "https://codeclaritylab.com/glossary/array_data_structure",
        "json_url": "https://codeclaritylab.com/glossary/array_data_structure.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": "[Arrays](https://codeclaritylab.com/glossary/array_data_structure) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/array_data_structure"
            }
        }
    }
}