CodeMIRAI
0 XP

Indexed arrays

2 min read 7 runnable examples Quiz included

An indexed array is a list of values, numbered from 0.

PHP
<?php
$fruits = ["mango", "apple", "guava"];
echo $fruits[0], "\n";               // mango
echo count($fruits), " fruits\n";
$fruits[] = "banana";                // add to the end
$fruits[1] = "pear";                 // replace
print_r($fruits);
var_dump($fruits);

print_r shows an array's structure readably; var_dump adds every value's type and length, which helps when debugging.

Adding and removing

PHP
<?php
$queue = ["Asha", "Leo"];
array_push($queue, "Mei", "Raj");     // add to the end (like $queue[] = ...)
array_unshift($queue, "Zoe");         // add to the front
$last = array_pop($queue);            // remove from the end
$first = array_shift($queue);         // remove from the front
echo "removed $first and $last\n";
print_r($queue);

unset($queue[0]);                     // remove by index: leaves a gap
print_r($queue);
print_r(array_values($queue));        // renumber from 0

unset removes an element but doesn't renumber the rest, so the array starts at index 1 afterwards. array_values renumbers it.

Looping

PHP
<?php
$scores = [72, 95, 88, 61];
foreach ($scores as $i => $score) {
    echo "Student ", $i + 1, ": $score\n";
}
echo "Highest: ", max($scores), ", average: ", array_sum($scores) / count($scores), "\n";

Searching

PHP
<?php
$langs = ["PHP", "Python", "SQL"];
var_dump(in_array("SQL", $langs));
var_dump(array_search("Python", $langs));     // its index
var_dump(array_search("Ruby", $langs));       // false: not found

var_dump(in_array("1", [1, 2, 3]));           // true: loose comparison
var_dump(in_array("1", [1, 2, 3], true));     // false: strict

in_array and array_search compare loosely unless you pass true as the third argument, which is usually what you want.

Sorting

PHP
<?php
$nums = [42, 7, 19, 3];
sort($nums);              // changes the array itself
print_r($nums);
rsort($nums);             // largest first
echo implode(", ", $nums), "\n";

$words = ["banana", "apple", "Cherry"];
sort($words);
echo implode(", ", $words), "\n";               // capitals sort first: Cherry leads
sort($words, SORT_FLAG_CASE | SORT_STRING);
echo implode(", ", $words), "\n";               // ignoring case

PHP's sort functions change the array in place and return true, so write sort($nums); on its own, never $nums = sort($nums);.

Arrays inside arrays

PHP
<?php
$grid = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
];
echo $grid[1][2], "\n";           // row 1, column 2: 6
foreach ($grid as $row) {
    echo implode(" ", $row), "\n";
}

Destructuring and spreading

PHP
<?php
[$a, $b, $c] = [10, 20, 30];
echo "$a $b $c\n";

$x = 1;
$y = 2;
[$x, $y] = [$y, $x];                     // swap two variables
echo "$x $y\n";

[$min, , $max] = [3, 5, 9];              // skip a value
echo "$min $max\n";

$morning = ["tea", "toast"];
$evening = ["soup"];
$all = [...$morning, ...$evening, "water"];
print_r($all);

[$a, $b] = ... unpacks an array into variables, and ...$array spreads one array inside another.

Finished this lesson?Mark it complete to earn +20 XP and move your progress on.