Arrays
Arrays appeared briefly in Chapter 4's foreach examples. This chapter covers them properly: the two main styles PHP arrays come in, and the handful of built-in functions used constantly in real PHP code.
Indexed Arrays — Position-Based
$fruits[0] is the first element; $fruits[count($fruits) - 1] is the last.
Associative Arrays — Named Keys
An associative array uses meaningful string keys instead of numeric positions — genuinely more readable than remembering "index 1 is always the age" when modelling real-world data like a person's details.
Multidimensional Arrays — Arrays of Arrays
An array's elements can themselves be arrays — extremely common for representing lists of records, like rows that might later come from a database (covered in a future course on database integration).
Common Built-In Array Functions
| Function | What it does |
|---|---|
| count($arr) | Number of elements |
| array_push($arr, $val) | Adds to the end (same as $arr[] = $val) |
| array_pop($arr) | Removes and returns the last element |
| array_merge($a, $b) | Combines two arrays into one |
| in_array($val, $arr) | Checks whether a value exists in the array |
| array_search($val, $arr) | Returns the key/index of a value, or false if not found |
| sort($arr) | Sorts values, re-indexes keys from 0 |
| array_map($fn, $arr) | Applies a function to every element, returns a new array |
| array_filter($fn, $arr) | Keeps only elements where the function returns true |
function($n) { ... } passed in is a function with no name, defined right where it's used — this pattern (a "callback") is genuinely common in PHP once you start using these array functions. It will feel more natural with practice; for now, treat it as "the rule to apply to each element," written inline rather than as a separately-named function.
array_values().
Coding Challenges
Create an associative array representing a book with keys "title", "author", and "year". Echo a formatted sentence using all three values, then add a new key "genre" and echo the whole array with print_r.
Given an indexed array of 6 numbers of your choice, use in_array() to check if 42 is present, array_search() to find the index of a number you know is in there, and sort() to put the array in ascending order. Echo the result of each step.
Given an array of 8 numbers, use array_filter() with an anonymous function to keep only numbers greater than 10, then use array_map() on the filtered result to square each remaining number. Wrap the filtered result in array_values() before mapping, and print_r the final array.
Chapter 6 Quick Reference
- Indexed arrays — zero-based numeric positions: $arr[0] is the first element
- Associative arrays — named string keys: $arr["key"]
- Multidimensional arrays — arrays of arrays, accessed like $arr[0]["key"]
- count(), array_push/pop, array_merge, in_array, array_search, sort — everyday array operations
- array_map($fn, $arr) — transforms every element, returns a new array
- array_filter($fn, $arr) — keeps only matching elements, preserves original keys
- array_values($arr) — re-indexes an array from 0, useful after filtering
- Next chapter: strings — common string functions, formatting, and manipulation