PHPIndexed Arrays

Indexed Arrays

An indexed array is a PHP array whose keys are sequential integers, starting at 0. This is the default shape you get when you list values without specifying keys, and it is what most people mean when they informally say "array" in everyday conversation. Indexed arrays are the natural fit for ordered collections where position matters more than a label — a list of tags, a queue of jobs, rows read from a CSV file.

Zero-based numbering

The first element of an indexed array is always at key 0, not 1. This trips up beginners coming from spreadsheets or from everyday counting, so it is worth internalizing early: the key of the nth element is n - 1.

Zero-based keys

PHP
<?php
$colors = ["red", "green", "blue"];

echo $colors[0]; // red   - the first element
echo "\n";
echo $colors[2]; // blue  - the third element
red
blue
Appending: $arr[] and array_push()

The idiomatic way to add a new element to the end of an indexed array is the empty-bracket syntax $arr[] = $value. PHP looks at the highest existing integer key, adds one, and inserts the new value there. array_push() does the same thing but as a function call, and it accepts multiple values in one call. For a single append, the [] syntax is both faster and more idiomatic, since array_push() carries a small function-call overhead.

Two ways to append

PHP
<?php
$queue = ["first", "second"];

$queue[] = "third";               // preferred for a single value
array_push($queue, "fourth", "fifth"); // good for multiple values at once

print_r($queue);
Array
(
    [0] => first
    [1] => second
    [2] => third
    [3] => fourth
    [4] => fifth
)
Gaps in the keys after unset()

unset() removes an element by key, but it does not shift the remaining elements down or renumber anything. This leaves a gap in the sequence of integer keys, which surprises people who expect an array to behave like a dense list at all times.

unset() leaves a hole

PHP
<?php
$letters = ["a", "b", "c", "d"];
unset($letters[1]); // remove "b"

print_r($letters);
echo count($letters); // 3 - count reflects remaining elements, not the range
Array
(
    [0] => a
    [2] => c
    [3] => d
)
3
Gapped keys break naive index loops
After an `unset()`, a loop like `for ($i = 0; $i < count($arr); $i++) { echo $arr[$i]; }` will hit a missing key and emit an "Undefined array key" warning in PHP 8. Use `foreach`, or re-index the array first, whenever gaps are possible.
Restoring sequential keys with array_values()

array_values() returns a new array containing the same values but with fresh, sequential integer keys starting at 0. It is the standard fix after removing elements, or after filtering an array with a function like array_filter(), both of which can leave gaps behind.

Re-indexing after a removal

PHP
<?php
$letters = ["a", "b", "c", "d"];
unset($letters[1]);

$reindexed = array_values($letters);
print_r($reindexed);
Array
(
    [0] => a
    [1] => c
    [2] => d
)
  • Indexed arrays are still just PHP arrays under the hood - there is no separate "list" type, only a convention about which keys are used.

  • Mixing explicit numeric keys with [] appends can produce non-obvious results - PHP always appends after the current highest integer key, even if you assigned it far out of sequence.

  • array_is_list() (PHP 8.1+) tells you whether an array has purely sequential integer keys starting at 0, which is useful before assuming foreach order matches numeric position.

Negative and non-contiguous starting keys
You can seed an indexed array with a non-zero or even negative starting key, e.g. `$arr = [5 => "x"]; $arr[] = "y";` appends at key `6`, not `0`. PHP always continues from the current maximum integer key rather than resetting to zero.
Tip
Whenever an array might have had elements removed, call `array_values()` before relying on sequential indexes - it costs very little and prevents subtle "undefined array key" bugs later in the script.