TExcelTAD
Home › PHP › PHP Indexed Arrays

PHP Indexed Arrays

An indexed array is a plain list: PHP numbers the items for you, starting at 0. Use it whenever the order matters and the items have no natural name — a playlist, the lines of a file, the steps of a recipe.

Creating and reading

php · try it yourself
<?php
$steps = ['Boil water', 'Add pasta', 'Wait 9 minutes', 'Drain'];

echo 'First step: ' . $steps[0] . PHP_EOL;
echo 'Last step: ' . $steps[count($steps) - 1] . PHP_EOL;
echo 'Total steps: ' . count($steps) . PHP_EOL;
First step: Boil water
Last step: Drain
Total steps: 4

Because the first index is 0, the last one is always count($steps) - 1. Reading an index that does not exist produces a warning, so check with isset() or use the null coalescing operator: $steps[10] ?? 'none'.

Looping over an indexed array

foreach is the most common way. Add $i => when you also need the position.

php · try it yourself
<?php
$planets = ['Mercury', 'Venus', 'Earth'];

foreach ($planets as $planet) {
    echo $planet . PHP_EOL;
}

foreach ($planets as $i => $planet) {
    echo ($i + 1) . '. ' . $planet . PHP_EOL;
}

for ($i = count($planets) - 1; $i >= 0; $i--) {
    echo $planets[$i] . ' ';
}
echo PHP_EOL;
Mercury
Venus
Earth
1. Mercury
2. Venus
3. Earth
Earth Venus Mercury 

A classic for loop is useful when you need to walk backwards or skip items, as in the last loop above.

Adding and removing at the ends

Indexed arrays can act as a stack (last in, first out) or a queue (first in, first out) with four functions:

Function Works on Effect
array_push($a, $v) or $a[] = $v end add
array_pop($a) end remove and return
array_unshift($a, $v) start add (re-numbers keys)
array_shift($a) start remove and return (re-numbers keys)
php · try it yourself
<?php
$tasks = ['write', 'test'];
$tasks[] = 'deploy';
array_unshift($tasks, 'plan');

echo implode(' > ', $tasks) . PHP_EOL;

$done = array_shift($tasks);
$last = array_pop($tasks);

echo "Finished: $done, postponed: $last" . PHP_EOL;
echo implode(' > ', $tasks) . PHP_EOL;
plan > write > test > deploy
Finished: plan, postponed: deploy
write > test

Gaps in the keys

unset() removes an item but leaves a hole in the numbering. array_values() returns a fresh list with keys 0, 1, 2, … again. This matters when you later use a for loop or convert the array to JSON (an array with gaps becomes a JSON object).

php · try it yourself
<?php
$scores = [70, 85, 90, 60];
unset($scores[1]);

echo json_encode($scores) . PHP_EOL;

$scores = array_values($scores);
echo json_encode($scores) . PHP_EOL;
echo 'Is a list? ' . var_export(array_is_list($scores), true) . PHP_EOL;
{"0":70,"2":90,"3":60}
[70,90,60]
Is a list? true

Tip: array_is_list() (PHP 8.1+) tells you whether the keys are exactly 0, 1, 2, … in order.

Searching and slicing

Function Example Result
in_array('b', $a) $a = ['a', 'b'] true
array_search('b', $a) same 1 (the key)
array_slice($a, 1, 2) [10, 20, 30, 40] [20, 30]
array_reverse($a) [1, 2, 3] [3, 2, 1]
range(1, 5) — [1, 2, 3, 4, 5]

Note: array_search() returns false when nothing is found, but 0 when the value is at the first position. Always compare with !== false.

Test yourself

How do you get the last element of $a without removing it?

  1. $a[count($a)]
  2. $a[count($a) - 1]
  3. array_pop($a)
  4. $a[-1]

What does array_values() do?

  1. Sorts the values
  2. Returns only the keys
  3. Re-numbers the items with keys 0, 1, 2, …
  4. Removes duplicates

Which function removes and returns the FIRST item?

  1. array_pop()
  2. array_shift()
  3. array_unshift()
  4. unset()