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

Arrays

Data Structures PHP 5.0+ Beginner
debt(d5/e3/b5/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list Blackfire and php-meminfo as tools — these are profiling/memory-analysis tools that can reveal inefficient array usage (e.g., array_shift in loops, excessive memory from large numeric arrays). Default linters won't flag these; it requires specialist profiling to notice the performance impact.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix suggests replacing PHP arrays with SplFixedArray for numeric datasets, which is a localized refactor — swapping the data structure and adjusting access patterns. Common mistakes like array_unshift in a loop require replacing with a queue or linked list, which is a small but multi-line change within one component. Not a one-liner, but not cross-cutting either.

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

Closest to 'persistent productivity tax' (b5). PHP arrays are fundamental and ubiquitous across all contexts (web, cli, queue-worker). The hash-map-disguised-as-array nature pervades the entire codebase. Every developer must understand the performance characteristics. While you don't need to replace all arrays, the misunderstanding about their nature creates a persistent tax on performance-sensitive code paths across many work streams.

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

Closest to 'serious trap — contradicts how a similar concept works elsewhere' (t7). The misconception field is explicit: 'PHP arrays are arrays' — developers coming from C, Java, or any other language expect contiguous memory, O(1) index access with no hash overhead, and no key-value semantics. PHP arrays are actually ordered hash maps, which fundamentally contradicts the mental model from virtually every other mainstream language. This leads directly to the common mistakes (assuming array_shift is cheap, not realizing the memory overhead vs true arrays).

About DEBT scoring →

Also Known As

dynamic array fixed array ArrayList contiguous memory

TL;DR

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.

Explanation

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.

Common 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.

Code Examples

✗ Vulnerable
// O(n²) — array_unshift in a loop reindexes entire array each time:
$result = [];
foreach ($items as $item) {
    array_unshift($result, $item); // O(n) reindex — loop becomes O(n²)
}
✓ Fixed
// O(n) — append then reverse once:
$result = [];
foreach ($items as $item) {
    $result[] = $item; // O(1) append
}
$result = array_reverse($result); // O(n) once at end

// Or use SplStack / SplDoublyLinkedList for O(1) prepend:
$stack = new SplStack();
foreach ($items as $item) $stack->push($item);

Added 15 Mar 2026
Edited 22 Mar 2026
Views 79
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 2 pings T 4 pings F 3 pings S 4 pings S 5 pings M 1 ping T 1 ping W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 3 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 16 Scrapy 16 Perplexity 7 Google 6 Ahrefs 4 Unknown AI 3 ChatGPT 3 Claude 2 Bing 2 PetalBot 2 Meta AI 1
crawler 57 crawler_json 4 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ 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
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
foreach over large numeric array where SplFixedArray would use less memory; array_shift O(n) when dequeue needed
Auto-detectable: ✗ No blackfire php-meminfo
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Low Context: Function


✓ schema.org compliant