Tutorials › PHP Tutorial › PHP Arrays

PHP Arrays

15 min read · Lesson 14 of 24

An array stores many values in a single variable. Instead of $student1, $student2, $student3 and so on, you keep one $students array and loop over it. Arrays are everywhere in PHP: form data, database rows, settings and JSON all arrive as arrays.

PHP arrays come in two flavours, and both use the same underlying type:

  • Indexed arrays use numbers (0, 1, 2...) as keys.
  • Associative arrays use names (strings) as keys.

Arrays that contain other arrays are called multidimensional arrays.

Indexed arrays

Create an indexed array with square brackets. PHP numbers the items from 0.

Example PHP
<?php
$counties = ["Kisumu", "Nairobi", "Mombasa"];

echo $counties[0] . "\n";
echo $counties[2] . "\n";
echo "There are " . count($counties) . " counties in the list.";
Result
Kisumu
Mombasa
There are 3 counties in the list.
Open in editor
Note: You may see the older syntax array("Kisumu", "Nairobi") in existing code. It means exactly the same as the short [ ] syntax.

Adding and changing items

Write [] with no index to add an item to the end. Use an index to replace an existing item.

Example PHP
<?php
$fruits = ["mango", "banana"];
$fruits[] = "pawpaw";       // append
$fruits[0] = "avocado";     // replace the first item

print_r($fruits);
Result
Array
(
    [0] => avocado
    [1] => banana
    [2] => pawpaw
)
Open in editor

print_r() prints an array in a readable way. It is great for quick debugging; var_dump() gives even more detail, including types.

Looping through an indexed array

Example PHP
<?php
$marks = [72, 65, 88, 54];
$total = 0;

foreach ($marks as $i => $mark) {
    echo "Exam " . ($i + 1) . ": $mark\n";
    $total += $mark;
}
echo "Average: " . $total / count($marks);
Result
Exam 1: 72
Exam 2: 65
Exam 3: 88
Exam 4: 54
Average: 69.75
Open in editor

Associative arrays

An associative array uses meaningful keys, written with the => arrow. It is like a small record: each value has a label.

Example PHP
<?php
$student = [
    "name" => "Kevin Ouma",
    "admNo" => "SDA-0042",
    "course" => "PHP",
    "balance" => 1500,
];

echo $student["name"] . " is studying " . $student["course"] . ".\n";
$student["balance"] -= 500;
$student["phone"] = "0722 000 111";   // add a new key
echo "Balance: KSh {$student['balance']}\n";
echo "Fields: " . count($student);
Result
Kevin Ouma is studying PHP.
Balance: KSh 1000
Fields: 5
Open in editor
Tip: To print an array value inside a double-quoted string, wrap it in curly braces: "{$student['balance']}". This avoids confusing PHP with the quotes inside the brackets.

Looping with keys

Example PHP
<?php
$stock = ["Unga" => 24, "Sugar" => 0, "Rice" => 15];

foreach ($stock as $product => $qty) {
    $status = $qty > 0 ? "$qty in stock" : "OUT OF STOCK";
    echo "$product: $status\n";
}
Result
Unga: 24 in stock
Sugar: OUT OF STOCK
Rice: 15 in stock
Open in editor

Checking if a key exists

Reading a key that does not exist gives a warning. Check first with isset() or array_key_exists(), or supply a fallback with ??.

Example PHP
<?php
$config = ["siteName" => "Skope", "debug" => null];

var_dump(isset($config["siteName"]));
var_dump(isset($config["debug"]));
var_dump(array_key_exists("debug", $config));
echo $config["theme"] ?? "default theme";
Result
bool(true)
bool(false)
bool(true)
default theme
Open in editor

isset() returns false when the value is null, even if the key exists. array_key_exists() only checks that the key is there.

Multidimensional arrays

An array can hold other arrays. A list of associative arrays is the most common shape in PHP, because it is exactly what you get back from a database query.

Example PHP
<?php
$students = [
    ["name" => "Amina", "score" => 81],
    ["name" => "Brian", "score" => 67],
    ["name" => "Chebet", "score" => 74],
];

echo $students[1]["name"] . "\n";

foreach ($students as $s) {
    echo str_pad($s["name"], 8) . $s["score"] . "\n";
}
Result
Brian
Amina   81
Brian   67
Chebet  74
Open in editor

Removing items

unset() deletes an element by key. Note that an indexed array keeps its old numbers afterwards; use array_values() to renumber it.

Example PHP
<?php
$queue = ["Ali", "Beatrice", "Chris"];
unset($queue[1]);
print_r($queue);
print_r(array_values($queue));
Result
Array
(
    [0] => Ali
    [2] => Chris
)
Array
(
    [0] => Ali
    [1] => Chris
)
Open in editor

Destructuring

You can unpack array values into separate variables in one line.

Example PHP
<?php
[$first, $second] = ["Gold", "Silver"];
echo "$first then $second\n";

["name" => $who, "town" => $where] = ["name" => "Mercy", "town" => "Eldoret"];
echo "$who lives in $where";
Result
Gold then Silver
Mercy lives in Eldoret
Open in editor

The Array Functions lesson shows how to sort, search, filter and transform arrays.

Exercise

Print the second item of the array.

$days = ["Mon", "Tue", "Wed"];
echo $days[];
Exercise

Complete the associative array.

$car = ["make"  "Toyota", "year"  2019];
Exercise

Add "Thika" to the end of the array.

$towns = "Thika";
Sign in or create a free account to track progress, earn XP, badges and a certificate.