PHP array Keyword
last modified November 25, 2026
The PHP array keyword builds an ordered map of keys and values. It is a language construct rather than a function, and the short [] form has been the common syntax since PHP 5.4.
This article shows how the literal is written, how automatic integer keys are chosen, how PHP converts written keys to integers or strings, how arrays nest, and how assignment copies an array.
Basic definitions
The array keyword is a language construct that builds an ordered map: it pairs keys with values, keeps the insertion order, and allows any value type, including another array. It is not a function, so it cannot be redefined or used as a callback.
Two forms produce the same result. The long form is array(1, 2, 3) and the short form is [1, 2, 3], which has been available since PHP 5.4. The short form is the usual choice in modern code.
A key is either an integer or a string. When a key is omitted, PHP uses the next integer after the largest integer key used so far. String keys are written with =>, as in ["name" => "Alice"]. If the same key appears twice, the later value replaces the earlier one and the key keeps its original position.
This page covers the literal syntax and the rules for keys. The PHP array tutorial covers the operations in depth, and PHP array functions lists the built-in helpers.
Creating an array with array()
The first example uses the long form and reads the elements back by their numeric keys.
<?php declare(strict_types=1); $scores = array(72, 85, 91); echo "Elements: ", count($scores), "\n"; echo "First: ", $scores[0], "\n"; echo "Last: ", $scores[2], "\n";
The three values receive the keys 0, 1, and 2. count() returns the number of elements, and the square brackets read one element by key. Nothing in the declaration fixes the size: the array grows as elements are appended, and the keys do not have to be contiguous.
$ php arr_basic.php Elements: 3 First: 72 Last: 91
Automatic integer keys
The empty brackets append a value and let PHP choose the key. The rule is to take the largest integer key used so far and add one, so an append never fills a gap left by an explicit key.
<?php
declare(strict_types=1);
$queue = array(10, 20);
$queue[] = 30;
$queue[10] = 40;
$queue[] = 50;
echo "Keys: ", implode(", ", array_keys($queue)), "\n";
echo "Count: ", count($queue), "\n";
echo "Value at 11: ", $queue[11], "\n";
The first two values take 0 and 1. After $queue[10] = 40, the next append receives 11 rather than 3, because 10 is now the largest integer key. The resulting keys are 0, 1, 2, 10, and 11. Relying on this rule is safe, but inserting a high key and then appending can leave surprising gaps, so explicit keys are clearer when the numbers matter.
$ php arr_keys.php Keys: 0, 1, 2, 10, 11 Count: 5 Value at 11: 50
How keys are converted
Keys are not stored exactly as written. Array keys go through a small set of conversions before the value is stored.
<?php
declare(strict_types=1);
$data = [
"1" => "one",
"01" => "zero-one",
true => "true",
false => "false",
];
var_dump(array_keys($data));
echo "Value at 1: ", $data[1], "\n";
echo "Value at 01: ", $data["01"], "\n";
echo "Value at 0: ", $data[0], "\n";
The string "1" becomes the integer 1. The string "01" stays a string, because it is not a canonical decimal integer. The boolean true becomes 1, so it overwrites the earlier value at that key, and false becomes 0. The keys in the result are therefore 1, "01", and 0, in insertion order. The value at key 1 is "true", not "one", because the later assignment won.
The conversion explains two common surprises: $a[1] and $a["1"] reach the same element, while $a["01"] is a separate element, and a boolean key usually hides a mistake in the code.
$ php arr_key_types.php
array(3) {
[0]=>
int(1)
[1]=>
string(2) "01"
[2]=>
int(0)
}
Value at 1: true
Value at 01: zero-one
Value at 0: false
Nested arrays
An array element can hold another array, which is how PHP represents tables, trees, and JSON-like structures. Access chains one pair of brackets per level.
<?php
declare(strict_types=1);
$week = [
"Mon" => [12, 14, 9],
"Tue" => [15, 17, 11],
];
$week["Wed"] = [10, 13, 8];
echo "Tuesday noon: ", $week["Tue"][1], "\n";
foreach ($week as $day => $temps) {
echo $day, ": ", implode(", ", $temps), "\n";
}
echo "Days: ", count($week), "\n";
The outer array maps a day name to a list of temperatures. $week["Tue"][1] first selects the inner array and then its second element. The foreach loop walks the outer level and prints each inner level with implode(). Adding Wed shows that the structure is not fixed: a new day is just another key-value pair.
$ php arr_nested.php Tuesday noon: 17 Mon: 12, 14, 9 Tue: 15, 17, 11 Wed: 10, 13, 8 Days: 3
The short syntax
Square brackets replace the array() keyword and work for indexed, associative, and nested arrays alike.
<?php
declare(strict_types=1);
$legacy = array("red", "green");
$modern = ["red", "green"];
var_dump($legacy === $modern);
$user = [
"name" => "Alice",
"roles" => ["admin", "editor"],
];
echo $user["name"], " has ", count($user["roles"]), " roles\n";
The strict comparison $legacy === $modern is true: the two forms build exactly the same array, with the same keys, values, and order. The short form also nests without extra noise, as the roles list shows. Mixing both forms in one project works, but a consistent style is easier to read.
$ php arr_short.php bool(true) Alice has 2 roles
Copies and references
An array is a value, not an object handle. Assigning it to another variable copies it, and a reference is needed to make two variables share one array.
<?php declare(strict_types=1); $original = ["a", "b"]; $copy = $original; $copy[] = "c"; echo "Original: ", count($original), " elements\n"; echo "Copy: ", count($copy), " elements\n"; $shared = &$original; $shared[] = "d"; echo "Original after reference: ", count($original), " elements\n";
Appending "c" to $copy leaves $original with two elements. The reference created with & does the opposite: $shared and $original are two names for the same array, so appending through one is visible through the other. Passing an array to a function follows the copy rule as well, unless the parameter is declared by reference. The copy by value and reference tutorial explains the general mechanism.
$ php arr_copy.php Original: 2 elements Copy: 3 elements Original after reference: 3 elements
Best practices
Use the short syntax and make the shape of the array obvious from the code.
- Prefer
[]overarray()for new code. - Do not rely on implicit key conversion; use the key type you actually mean.
- Append with
$a[]instead of computing the next integer by hand. - Keep one level of meaning per array, and name the variable after the whole structure.
- Remember that assignment copies an array; use a reference only when sharing is intended.
The PHP array tutorial continues with initialization, slicing, merging, and traversal, and PHP foreach keyword shows the usual way to walk an array.
Source
This article covered the PHP array keyword: the long and short forms, automatic and explicit keys, key conversion, nested arrays, and copy semantics. Continue with PHP array for the operations, or read PHP array functions for the built-in helpers.
Author
List all PHP tutorials.