array()
The array() function in PHP is used to create an array. An array is a variable that can store multiple values of the same data type. In this article, we will
Introduction
The array() language construct creates a PHP array — an ordered map that pairs keys with values. Despite the name "array," a PHP array is far more flexible than a fixed-size array in languages like C or Java: a single array can hold mixed value types, grow or shrink at runtime, and use either integers or strings as keys.
This page covers how array() works, the three kinds of arrays you'll build with it, the modern [] short syntax, and the everyday operations (counting, adding, looping) you'll reach for most. Each example is runnable.
Basic syntax
array(value1, value2, value3, ...)
// or, since PHP 5.4, the short syntax:
[value1, value2, value3, ...]You can also assign keys explicitly with the key => value arrow:
array(key1 => value1, key2 => value2, ...)Both forms return an array value. The two syntaxes are interchangeable; the short [] form is preferred in modern code.
Creating an array
When you don't supply keys, PHP assigns sequential integer keys starting at 0. So $fruits[0] is "apple", $fruits[1] is "banana", and $fruits[2] is "orange". This is an indexed array.
Array
(
[0] => apple
[1] => banana
[2] => orange
)The three kinds of arrays
PHP uses one data structure, array, for three conceptual shapes:
Indexed arrays
Keys are integers assigned automatically. Use these for ordered lists where position is all that matters.
<?php
$colors = ["red", "green", "blue"];
echo $colors[1]; // greenSee indexed arrays for more.
Associative arrays
You choose string keys to label each value, which is ideal for records and lookups.
<?php
$user = [
"name" => "Ada",
"email" => "[email protected]",
"age" => 36,
];
echo $user["email"]; // [email protected]See associative arrays for more.
Multidimensional arrays
A value can itself be an array, letting you model tables and nested data.
<?php
$matrix = [
[1, 2, 3],
[4, 5, 6],
];
echo $matrix[1][2]; // 6See multidimensional arrays for more.
Counting elements
The count() function returns how many top-level elements an array holds. It pairs naturally with array():
<?php
$fruits = array("apple", "banana", "orange");
echo count($fruits); // 3Adding elements
To append a value, use the [] syntax — it's shorter than calling array_push() and clearer for a single element:
<?php
$fruits = ["apple", "banana"];
$fruits[] = "cherry"; // appended with the next integer key (2)
$fruits["fav"] = "mango"; // added with an explicit string key
print_r($fruits);Array
(
[0] => apple
[1] => banana
[2] => cherry
[fav] => mango
)This mix of integer and string keys in one array is perfectly valid — it is what makes PHP arrays "ordered maps."
Looping over an array
foreach is the idiomatic way to walk every key/value pair, and it works for all three array kinds:
<?php
$user = ["name" => "Ada", "age" => 36];
foreach ($user as $key => $value) {
echo "$key: $value\n";
}name: Ada
age: 36See the foreach loop for the full reference.
Common gotchas
- Duplicate keys overwrite.
[1 => "a", 1 => "b"]keeps only"b"— the last value assigned to a key wins. - String keys that look like integers become integers.
["7" => "x"]is stored under integer key7, so$a[7]and$a["7"]reach the same element. - Accessing a missing key emits a warning and yields
null. Guard witharray_key_exists()orisset()first. - Counting only counts the top level.
count()on a multidimensional array returns the number of outer elements, not the total leaf count.
Best practices
- Prefer the short
[]syntax overarray()in new code — it is the modern convention. - Use meaningful string keys for associative data so the code reads like the domain it models.
- Append with
$arr[] = ...rather thanarray_push()when adding a single value. - Sort with the right function for your key type — see sorting arrays for
sort(),asort(),ksort(), and friends.
Conclusion
The array() construct (and its [] shorthand) is the foundation of data handling in PHP. Because one array type doubles as an indexed list, an associative map, and a nestable tree, mastering creation, counting, appending, and looping unlocks most of the language's data-manipulation toolkit.