Arrays
⏱ 15 min read read
PHP Arrays:
PHP arrays are ordered maps --- they can act as indexed arrays,
associative arrays (like Python dicts), or both at once. They are PHP's
most powerful built-in data structure.
// Indexed array (0-based)
$fruits = ["Apple", "Banana", "Cherry"];
// Associative array (key => value)
$person = ["name" => "Alice", "age" => 25, "city" =>
"Lagos"];
// Mixed (both numeric and string keys)
$mixed = [0 => "first", "name" => "Alice", 1 =>
"second"];
Common Array Functions:
count($arr) // number of elements
array_push($arr, "item") // add to end
array_pop($arr) // remove and return last
array_shift($arr) // remove and return first
array_unshift($arr, "item") // add to beginning
in_array("Apple", $arr) // check if value exists
array_key_exists('name', $person) // check key
array_keys($assoc) // get all keys
array_values($assoc) // get all values
array_merge($a, $b) // merge two arrays
array_slice($arr, 1, 3) // sub-array (start, length)
array_splice($arr, 1, 2, ['x']) // remove and replace
Sorting:
sort($arr) // sort indexed array in place (loses keys)
rsort($arr) // reverse sort
asort($assoc) // sort by value, keep keys
arsort($assoc) // reverse sort by value, keep keys
ksort($assoc) // sort by key
krsort($assoc) // reverse sort by key
usort($arr, fn($a,$b) => $a <=> $b); // custom sort
IMPORTANT: PHP sort() modifies the array in place (like Python's
list.sort()).
PHP has NO immutable sorted() equivalent that returns new array.
Use: $sorted = $arr; sort($sorted); to keep original.
PHP arrays are ordered maps --- they can act as indexed arrays,
associative arrays (like Python dicts), or both at once. They are PHP's
most powerful built-in data structure.
// Indexed array (0-based)
$fruits = ["Apple", "Banana", "Cherry"];
// Associative array (key => value)
$person = ["name" => "Alice", "age" => 25, "city" =>
"Lagos"];
// Mixed (both numeric and string keys)
$mixed = [0 => "first", "name" => "Alice", 1 =>
"second"];
Common Array Functions:
count($arr) // number of elements
array_push($arr, "item") // add to end
array_pop($arr) // remove and return last
array_shift($arr) // remove and return first
array_unshift($arr, "item") // add to beginning
in_array("Apple", $arr) // check if value exists
array_key_exists('name', $person) // check key
array_keys($assoc) // get all keys
array_values($assoc) // get all values
array_merge($a, $b) // merge two arrays
array_slice($arr, 1, 3) // sub-array (start, length)
array_splice($arr, 1, 2, ['x']) // remove and replace
Sorting:
sort($arr) // sort indexed array in place (loses keys)
rsort($arr) // reverse sort
asort($assoc) // sort by value, keep keys
arsort($assoc) // reverse sort by value, keep keys
ksort($assoc) // sort by key
krsort($assoc) // reverse sort by key
usort($arr, fn($a,$b) => $a <=> $b); // custom sort
IMPORTANT: PHP sort() modifies the array in place (like Python's
list.sort()).
PHP has NO immutable sorted() equivalent that returns new array.
Use: $sorted = $arr; sort($sorted); to keep original.
Log in to track your progress and earn badges as you complete lessons.
Log In to Track Progress