PHP Reference ยท Types and Type Handling
The PHP array type
Reference for the PHP array type: ordered maps, list and associative syntax, key normalization, nesting, copy-on-write value semantics and common mistakes.
A PHP array is an ordered map: it stores key-value pairs and remembers the order in which you inserted them. Keys are integers or strings. The same type serves as a list, a dictionary, a stack, a queue and a set. Values can be of any type, including other arrays.
Arrays are values. Assigning one to another variable, or passing it to a function, behaves like a copy (PHP actually shares the memory until one side changes, which is called copy-on-write). Objects, in contrast, are handles.
$list = [10, 20, 30]; $map = ["name" => "Ada", "age" => 36]; $list[] = 40; // append
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
$a = []; $a = array(1, 2); // older form $a = [1, 2, 3]; $a = ["k" => "v"]; $a[] = $value; $a[$key] = $value; $value = $a[$key]; $value = $a[$key] ?? $default; $copy = [...$a, ...$b]; [$x, $y] = $pair;
| Key given | Stored as |
|---|---|
"5" (decimal integer string) |
int 5 |
"05", "1.5", " 5" |
string key |
5.7 |
int 5 (fraction dropped, deprecated since 8.1) |
true / false |
int 1 / 0 |
null |
"" (empty string; deprecated in PHP 8.5) |
| array or object | TypeError |
If no key is given, PHP uses the largest integer key so far plus one (or 0). A duplicate key overwrites the earlier value.
Usage notes
- Order is insertion order, not key order. Use
ksort()orsort()to change it. - Missing keys warn. Reading
$a["nope"]emits a warning and givesnull. Use??,isset()orarray_key_exists(); the last one is the only one that tellsnullvalues from missing keys. - Copy-on-write. Passing a big array to a function is cheap until you modify it inside. Use
&only when you really want to change the caller's array. foreachby reference leaves a dangling reference. Alwaysunset($n)after the loop, or later loops will overwrite the last element.array_is_list()(8.1) tells whether keys are0..n-1in order, which matters for JSON: lists encode as[], others as{}.- Spread and destructuring:
[...$a, ...$b]merges (string keys allowed since 8.1) and[$x, $y] = $aunpacks. See Array unpacking. - Comparison.
==compares key/value pairs;===also requires the same order and types;+unions by key. - Arrays are not objects. There is no
$a->count(); use functions such ascount()or wrap inArrayObject. For huge integer-indexed data,SplFixedArrayuses less memory. - Memory. Each element costs roughly 16 bytes in a packed list and more in a hash map; generators help with very large sequences.