PHP Arrays: List and Map in One
PHP's single array type, which doubles as both an ordered list and a string-keyed map.
What you'll learn
- Build an indexed array (a list) and an associative array (a map) using PHP's one array type
- Append to an indexed array with the `[]` syntax
- Predict the output of code that reads, counts, and joins array elements
Explanation
PHP has exactly one array type, and it genuinely serves two roles other languages split across separate structures: an indexed (ordered) list, where PHP auto-assigns integer keys starting at 0 ($fruits = ["apple", "banana", "cherry"];), and an associative array (a map/dictionary), where you supply your own keys, usually strings ($ages = ["Ada" => 30, "Grace" => 45];). Both are the same underlying type -- there's no separate "list" or "dict" class to choose between.
You append to an indexed array with the [] syntax: $fruits[] = "date"; adds a new element at the next available integer index. For an associative array, you assign directly to a new key: $ages["Linus"] = 55;.
Because both shapes are the same type, the same functions work on either: count($array) returns the number of elements regardless of whether the keys are integers or strings, and foreach iterates either shape (with as $key => $value when you need the keys). implode(", ", $array) joins an array's values into a string with a separator, commonly used with indexed arrays.
This dual nature is genuinely distinctive to PHP -- it's convenient (one mental model covers both use cases), but it also means "is this array a list or a map?" is a question you answer by looking at how it's being used, not by its declared type.
Guided lab
Fill in the blank: counting an associative array
Fill in the missing function name, then predict the output.
<?php
$fruits = ["apple", "banana", "cherry"];
$fruits[] = "date";
$ages = ["Ada" => 30, "Grace" => 45];
$ages["Linus"] = 55;
echo "Fruits: " . implode(", ", $fruits) . "\n";
echo "Ada's age: " . $ages["Ada"] . "\n";
echo "Count of ages: " . ____($ages) . "\n";Stuck? Get a hint.
Common mistakes
- Assuming PHP has separate list and dictionary types the way Python does -- it has one array type that covers both roles.
- Forgetting that `$array[] = $value` appends at the next integer index, and using it by mistake on an array you intended to keep purely associative.
- Calling `implode` on an associative array expecting the keys to appear -- `implode` joins the *values* only.
Knowledge check
Takeaway
PHP's one array type covers both ordered lists (auto-indexed, appended with `[]`) and associative maps (explicit string keys) -- the same functions like `count()` and `foreach` work on either.
Summary
Indexed arrays get auto-assigned integer keys and grow with `[]`; associative arrays use explicit keys; both are the same array type, so `count`, `foreach`, and `implode` (values only) work on either.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.