15. PHP Arrays
What you will learn
- How to create indexed arrays
- How to read, add, and remove items
- How to sort and search
- Useful array functions
What is an array?
An array stores several values in one variable. An indexed array uses numbers as positions, starting at 0.
PHP code
<?php
$courses = ["HTML", "CSS", "JavaScript"];
echo $courses[0] . "<br>";
echo "Count: " . count($courses) . "<br>";
echo "Last: " . $courses[count($courses) - 1];
What the browser shows
HTML
Count: 3
Last: JavaScript
Changing, adding, and removing
| Code | What it does |
|---|---|
$a[1] = "x"; |
Change an item |
$a[] = "x"; or array_push($a, "x") |
Add to the end |
array_pop($a) |
Remove and return the last item |
array_unshift($a, "x") |
Add to the start |
array_shift($a) |
Remove and return the first item |
unset($a[1]) |
Remove an item (keeps the other positions) |
PHP code
<?php
$list = ["b", "c"];
$list[] = "d";
array_unshift($list, "a");
$last = array_pop($list);
echo implode(", ", $list) . " | popped: " . $last;
Output
a, b, c | popped: d
Printing an array
You cannot echo a whole array. Use implode() to make text, or print_r() and var_dump() while debugging.
Searching
PHP code
<?php
$colors = ["red", "green", "blue"];
var_dump(in_array("green", $colors, true));
var_dump(array_search("blue", $colors, true));
var_dump(array_search("pink", $colors, true));
What you should see (inside <pre>)
bool(true) int(2) bool(false)
Pass true as the last argument for strict comparison. array_search() returns false when nothing is found, and can return 0 for a match at the start, so test it with !== false.
Sorting
| Function | Sorts |
|---|---|
sort($a) |
Values, low to high |
rsort($a) |
Values, high to low |
PHP code
<?php
$nums = [10, 9, 100, 1];
sort($nums);
echo implode(", ", $nums) . "<br>";
rsort($nums);
echo implode(", ", $nums);
What the browser shows
1, 9, 10, 100
100, 10, 9, 1
These functions change the original array. PHP sorts numbers as numbers, unlike JavaScript’s default.
Useful array functions
PHP code
<?php
$nums = [3, 8, 2, 8, 5];
echo array_sum($nums) . "<br>";
echo implode(",", array_unique($nums)) . "<br>";
echo implode(",", array_reverse($nums)) . "<br>";
echo implode(",", array_slice($nums, 1, 3)) . "<br>";
echo implode(",", array_filter($nums, fn($n) => $n > 4)) . "<br>";
echo implode(",", array_merge([1, 2], [3, 4]));
What the browser shows
26
3,8,2,5
5,8,2,8,3
8,2,8
8,8,5
1,2,3,4
array_sumadds the values, andarray_uniqueremoves duplicates.array_slice($a, start, length)copies part of an array.array_filterkeeps items that pass a test, andarray_mergejoins arrays.
Arrays of arrays
An item can itself be an array, making a table of data.
PHP code
<?php
$grid = [
[1, 2, 3],
[4, 5, 6]
];
echo $grid[1][2];
Output
6
Row 1, column 2 (counting from 0) is the value 6.
Common mistakes
- Counting from 1. The first item is at index 0.
- Trying to
echoan array, which prints the word “Array” with a warning. - Reading an index that does not exist.
- Forgetting that
unset()leaves a gap in the numbers. - Using
array_search()without!== false.
Exercise
Start with $todo = ["Wash up", "Study"];. Add Exercise to the end, remove the first item, and print the rest joined with " | ".
Show solution
<?php
$todo = ["Wash up", "Study"];
$todo[] = "Exercise";
array_shift($todo);
echo implode(" | ", $todo);
The output is Study | Exercise.
Knowledge check
1. What is the index of the first item?
0.
2. Which function returns how many items an array has?
count()
3. How do you add an item to the end of an array?
$array[] = $value; (or array_push).
Summary
- Indexed arrays hold lists, with positions starting at 0.
- Use
count,implode,in_array,sort,array_filter, and friends. - Arrays can contain other arrays.