Home Quizzes Leaderboard Competitions Learn Hire Us
About Contact
Log In Sign Up
Learn PHP Arrays

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.
Code Example
<?php

// Indexed array

$fruits = ["Apple", "Banana", "Cherry", "Mango"];

echo $fruits[0] . "\n"; // Apple

echo $fruits[count($fruits) - 1] . "\n"; // Mango

echo count($fruits) . "\n"; // 4

// Add / remove

array_push($fruits, "Grape");

$last = array_pop($fruits);

echo "Removed: $last\n";

// Loop

$numbers = [10, 25, 8, 42, 17];

$total = array_sum($numbers);

echo "Total: $total\n";

echo "Average: " . ($total / count($numbers)) . "\n";

// Sort

$grades = [72, 88, 55, 91, 67];

sort($grades);

echo "Sorted: " . implode(", ", $grades) . "\n";

echo "Min: " . min($grades) . "\n";

echo "Max: " . max($grades) . "\n";

// Associative array

$person = ["name" => "Alice", "age" => 25, "city" =>
"Lagos"];

echo $person['name'] . "\n"; // Alice

// Loop over associative

foreach ($person as $key => $value) {

echo "$key: $value\n";

}

// Check existence

echo in_array("Alice", $fruits) ? "found\n" : "not found\n";

echo array_key_exists('name', $person) ? "key exists\n" : "no
key\n";

// Multidimensional array

$students = [

["name" => "Alice", "grade" => 92],

["name" => "Bob", "grade" => 78],

];

foreach ($students as $s) {

echo "{$s['name']}: {$s['grade']}\n";

}

// array_filter --- keep elements matching condition

$passing = array_filter($grades, fn($g) => $g >= 70);

echo implode(", ", $passing) . "\n";

?>
← Loops Functions →

Log in to track your progress and earn badges as you complete lessons.

Log In to Track Progress