Arrays in PHP
Working with PHP's single, flexible array type — indexed, associative, and the built-in functions that manipulate them.
2 min read
Arrays are PHP's workhorse data structure — there's no separate "list" or "dictionary" type, just one array that can behave as either.
Creating arrays
<?php
$colors = ["red", "green", "blue"];
$colors2 = array("red", "green", "blue"); // older syntax, same result
$person = [
"name" => "Amara",
"age" => 28,
];[...] (short array syntax) is the modern standard; array(...) still works but is rarely written in new code. Keys are optional — omit them for a plain indexed list, or supply your own with => for an associative array.
Adding, accessing, and removing elements
<?php
$colors = ["red", "green"];
$colors[] = "blue"; // append to the end
echo $colors[0]; // "red"
unset($colors[1]); // remove "green" — leaves a gap in the numeric keys
$colors = array_values($colors); // re-index from 0 if a clean sequence matters$colors[] = ... is the idiomatic way to append. unset() removes an element by key but doesn't renumber the remaining ones — array_values() resets the keys to a clean 0, 1, 2... sequence afterward, when that matters.
Common array functions
<?php
$numbers = [3, 1, 4, 1, 5];
echo count($numbers); // 5
sort($numbers); // [1, 1, 3, 4, 5], sorted in place
print_r(array_unique($numbers)); // duplicates removed
echo array_sum($numbers); // sum of all elements
var_dump(in_array(4, $numbers)); // truecount(), sort(), array_unique(), array_sum(), and in_array() cover most everyday needs. Note that sort() (and its relatives like rsort, asort) modify the array in place and return true/false, not the sorted array — a common source of confusion for anyone expecting a functional-style return value.
Transforming arrays: map, filter, reduce
<?php
$prices = [10, 25, 5, 40];
$withTax = array_map(fn($price) => $price * 1.1, $prices);
$expensive = array_filter($prices, fn($price) => $price > 10);
$total = array_reduce($prices, fn($carry, $price) => $carry + $price, 0);array_map() transforms every element, array_filter() keeps only elements matching a condition (and preserves original keys, which often means following it with array_values()), and array_reduce() folds the array down to a single value, starting from an initial value (0 here). All three take a callback — fn(...) => ... is PHP's compact arrow-function syntax, covered in more detail in the functions lessons.
Multidimensional arrays
<?php
$users = [
["name" => "Amara", "role" => "admin"],
["name" => "Diego", "role" => "editor"],
];
foreach ($users as $user) {
echo "{$user['name']} — {$user['role']}\n";
}An array can hold other arrays as its values — this is how PHP represents anything table-shaped, and it's exactly the structure you get back from most database query results before they're turned into objects.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.