Skip to content
Tutorials Center
My learning

Topics

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.

PHP
$list = [10, 20, 30];
$map = ["name" => "Ada", "age" => 36];
$list[] = 40;           // append

Try it

PHP Lists, maps and key normalization

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.

PHP Value semantics, nesting and checking keys

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

PHP
$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() or sort() to change it.
  • Missing keys warn. Reading $a["nope"] emits a warning and gives null. Use ??, isset() or array_key_exists(); the last one is the only one that tells null values 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.
  • foreach by reference leaves a dangling reference. Always unset($n) after the loop, or later loops will overwrite the last element.
  • array_is_list() (8.1) tells whether keys are 0..n-1 in 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] = $a unpacks. 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 as count() or wrap in ArrayObject. For huge integer-indexed data, SplFixedArray uses 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.