Creating arrays
$list = ["apple", "pear", "plum"]; // indexed (0, 1, 2)
$user = ["name" => "Anna", "age" => 30]; // associative
$grid = [[1, 2], [3, 4]]; // nested
$nums = range(1, 5); // [1, 2, 3, 4, 5]
$zeros = array_fill(0, 3, 0); // [0, 0, 0]
[$a, $b] = [1, 2]; // destructuring
['name' => $name] = $user;Adding and removing
| Function | Effect |
|---|---|
$list[] = "fig"; | append (fastest) |
array_push($list, "a", "b") | append several |
array_pop($list) | remove and return last |
array_unshift($list, "x") | prepend |
array_shift($list) | remove and return first (re-indexes) |
unset($list[1]) | remove by key (keeps other keys) |
array_splice($list, 1, 2) | remove/replace a range (re-indexes) |
Information and searching
| Function | Returns |
|---|---|
count($arr) | number of elements |
in_array("pear", $list, true) | bool – always pass true for strict |
array_search("pear", $list, true) | key or false |
array_key_exists("age", $user) | bool, also true if value is null |
isset($user["age"]) | bool, false if value is null |
array_keys($arr) / array_values($arr) | keys / values as list |
array_key_first / array_key_last | first / last key |
array_is_list($arr) | keys are 0,1,2… (PHP 8.1) |
array_sum / array_product | sum / product |
max($arr) / min($arr) | largest / smallest value |
Combining and slicing
| Function | Example |
|---|---|
array_merge($a, $b) | join; string keys are overwritten, numeric keys renumbered |
[...$a, ...$b] | spread, same idea |
$a + $b | union: keeps keys from $a |
array_slice($arr, 1, 2) | part of an array (no change to original) |
array_combine($keys, $values) | build associative array |
array_flip($arr) | swap keys and values |
array_unique($arr) | remove duplicates |
array_diff($a, $b) | values in $a not in $b |
array_intersect($a, $b) | values in both |
array_chunk($arr, 3) | split into groups of 3 |
array_reverse($arr) | reverse order |
Transforming
$prices = [10, 20, 30];
$gross = array_map(fn($p) => $p * 1.19, $prices); // apply to each
$big = array_filter($prices, fn($p) => $p > 15); // keep matching (keys kept!)
$big = array_values($big); // renumber if needed
$total = array_reduce($prices, fn($c, $p) => $c + $p, 0); // reduce to one value
$users = [["id" => 1, "name" => "Anna"], ["id" => 2, "name" => "Ben"]];
$names = array_column($users, "name"); // ["Anna", "Ben"]
$byId = array_column($users, null, "id"); // re-keyed by id
array_walk($user, function (&$v, $k) { $v = trim($v); }); // change in placearray_filter without callback removes all falsy values (0, "", null, false).
Sorting
All sort functions change the array in place and return true – don't assign the result.
| Function | Sorts by | Keeps keys |
|---|---|---|
sort / rsort | value asc / desc | no |
asort / arsort | value asc / desc | yes |
ksort / krsort | key asc / desc | yes |
usort($a, $fn) | own comparison | no |
uasort / uksort | own comparison by value / key | yes |
natsort / natcasesort | "natural" order (img2 before img10) | yes |
shuffle | random | no |
usort($users, fn($a, $b) => $a['name'] <=> $b['name']);
usort($users, fn($a, $b) => [$b['age'], $a['name']] <=> [$a['age'], $b['name']]); // age desc, name ascStrings and arrays
explode, implode and str_split convert between them – see PHP String Functions. compact('a', 'b') builds ['a' => $a, 'b' => $b]; extract($arr) does the reverse (avoid with user input).
Previous: PHP String Functions · Next: PHP Math and Number Functions
Put together with AI as a quick reference I keep for my own work. Entries like this are usually not reviewed in detail by a person.