W3docs

Indexed Arrays

PHP is a powerful and popular server-side scripting language that is widely used for web development. One of its key features is the ability to work with

PHP Indexed Arrays

An indexed array is the simplest kind of PHP array: a list of values where each item is automatically given a numeric key (its position), starting at 0. Use one whenever you have an ordered collection — a list of products, the lines of a file, the rows of a result set — and you don't need a meaningful name for each slot.

PHP has three array types in total: indexed arrays (numeric keys, covered here), associative arrays (named string keys), and multidimensional arrays (arrays of arrays). This page covers how to create indexed arrays and how to read, change, add, count, and loop over their values.

Creating an Indexed Array

There are two equivalent ways to build an indexed array. The short [] syntax (PHP 5.4+) is the modern, preferred form; the array() function works on every PHP version.

// Short syntax (recommended)
$cars = ["Volvo", "BMW", "Toyota"];

// Long syntax — identical result
$cars = array("Volvo", "BMW", "Toyota");

Either way, PHP assigns the keys for you: "Volvo" gets index 0, "BMW" gets 1, and "Toyota" gets 2. An empty array is just $cars = [];, ready to have items pushed into it later.

Accessing Values

Read a value by putting its index in square brackets. Remember that the first element is at index 0, so the index of the last element is always one less than the number of items.

$cars = ["Volvo", "BMW", "Toyota"];

echo $cars[0]; // Volvo
echo $cars[2]; // Toyota

Accessing a key that does not exist (for example $cars[9]) returns null and emits a Warning: Undefined array key notice — guard with in_array() or isset() when you are not sure a key is present.

Modifying Values

Assign a new value to an existing index to overwrite it in place:

$cars = ["Volvo", "BMW", "Toyota"];
$cars[0] = "Mercedes";

echo $cars[0]; // Mercedes

The array still has three elements; only the value at index 0 changed.

Adding Values

To append to the end of the array, assign to the empty-bracket [] syntax — PHP picks the next available index automatically. The array_push() function does the same thing and can add several values at once:

$cars = ["Volvo", "BMW", "Toyota"];

$cars[] = "Audi";                 // index 3
array_push($cars, "Tesla", "Kia"); // indexes 4 and 5

print_r($cars);

This outputs:

Array
(
    [0] => Volvo
    [1] => BMW
    [2] => Toyota
    [3] => Audi
    [4] => Tesla
    [5] => Kia
)

Counting Elements

Use count() to find out how many items an array holds — useful for showing totals or as the upper bound of a loop:

$cars = ["Volvo", "BMW", "Toyota"];
echo count($cars); // 3

Looping Over an Indexed Array

The cleanest way to visit every value is a foreach loop, which works regardless of how many items there are:

$cars = ["Volvo", "BMW", "Toyota"];

foreach ($cars as $car) {
    echo $car . PHP_EOL;
}

Output:

Volvo
BMW
Toyota

If you also need each item's index, capture the key as well:

$cars = ["Volvo", "BMW", "Toyota"];

foreach ($cars as $index => $car) {
    echo "$index: $car" . PHP_EOL;
}
// 0: Volvo
// 1: BMW
// 2: Toyota

A classic for loop is an alternative when you want explicit numeric control:

$cars = ["Volvo", "BMW", "Toyota"];

for ($i = 0; $i < count($cars); $i++) {
    echo $cars[$i] . PHP_EOL;
}

Common Gotchas

  • Keys are not always sequential. Removing an element with unset($cars[1]) leaves a "hole" — the remaining keys stay 0 and 2. Call array_values() to re-index from 0 if you need a clean list.
  • Indexed vs. associative is fluid. Adding a string key ($cars["best"] = "Volvo") turns the array into a mixed/associative one. To keep an array purely indexed, only ever append or assign integer keys.
  • Sorting reorders, sometimes re-keys. Functions like sort() re-index the array from 0, which is usually what you want for an indexed list.

Conclusion

Indexed arrays store an ordered list of values under automatic numeric keys starting at 0. You create them with [], read and overwrite values by index, append with $arr[] or array_push(), measure them with count(), and iterate with foreach. When you need labelled rather than positional data, reach for an associative array instead.

Practice

Practice
Which of the following statements about Indexed Arrays in PHP are true?
Which of the following statements about Indexed Arrays in PHP are true?
Was this page helpful?