Aral Code

Arrays & Functions Lesson 10 of 17

PHP Array Functions — Sorting, Filtering and Mapping

Sort arrays with sort, rsort, asort, ksort and usort; summarize with array_sum and count; transform with array_map, array_filter, array_column and array_slice.

PHP comes with dozens of array functions. Knowing the main ones saves you from writing many loops.

Sorting

<?php
$grades = [88, 72, 95, 60, 81];
sort($grades);
echo implode(", ", $grades), "\n";
rsort($grades);
echo implode(", ", $grades), "\n";

$prices = ["Kape" => 8, "Pandesal" => 25, "Itlog" => 9];
asort($prices);      // by value, keep keys
print_r($prices);
ksort($prices);      // by key
echo implode(", ", array_keys($prices));

Output

60, 72, 81, 88, 95
95, 88, 81, 72, 60
Array
(
    [Kape] => 8
    [Itlog] => 9
    [Pandesal] => 25
)
Itlog, Kape, Pandesal
FunctionSorts byKeeps keys?
sort / rsortValue, up / downNo
asort / arsortValue, up / downYes
ksort / krsortKey, up / downYes
usortYour own ruleNo

Custom sorting with usort

<?php
$students = [
    ["name" => "Ana", "avg" => 91.5],
    ["name" => "Ben", "avg" => 84.0],
    ["name" => "Carla", "avg" => 95.2],
];
usort($students, fn($a, $b) => $b["avg"] <=> $a["avg"]);
foreach ($students as $i => $s) {
    echo ($i + 1) . ". {$s['name']} ({$s['avg']})\n";
}

Output

1. Carla (95.2)
2. Ana (91.5)
3. Ben (84)

fn($a, $b) => ... is an arrow function. The spaceship <=> returns -1, 0 or 1, exactly what usort needs. Swapping $a and $b sorts in descending order.

Summaries

<?php
$grades = [88, 72, 95, 60, 81];
echo "Count: " . count($grades), "\n";
echo "Sum: " . array_sum($grades), "\n";
echo "Average: " . round(array_sum($grades) / count($grades), 2), "\n";
echo "Highest: " . max($grades) . ", lowest: " . min($grades), "\n";
print_r(array_count_values(["Ana", "Ben", "Ana", "Ana"]));

Output

Count: 5
Sum: 396
Average: 79.2
Highest: 95, lowest: 60
Array
(
    [Ana] => 3
    [Ben] => 1
)

array_map, array_filter

<?php
$prices = [100, 250, 40];
$withVat = array_map(fn($p) => $p * 1.12, $prices);
echo implode(", ", $withVat), "\n";

$grades = [88, 72, 95, 60, 81];
$passing = array_filter($grades, fn($g) => $g >= 75);
echo implode(", ", $passing), "\n";
print_r(array_values($passing));

Output

112, 280, 44.8
88, 95, 81
Array
(
    [0] => 88
    [1] => 95
    [2] => 81
)

array_filter keeps the original keys (0, 2, 4); array_values renumbers them from 0.

array_column, array_slice, array_merge, array_unique

<?php
$products = [
    ["name" => "Pandesal", "price" => 25],
    ["name" => "Kape", "price" => 8],
    ["name" => "Itlog", "price" => 9],
];
echo implode(", ", array_column($products, "name")), "\n";
echo array_sum(array_column($products, "price")), "\n";

$top3 = array_slice([95, 90, 88, 81, 72], 0, 3);
echo implode(", ", $top3), "\n";

echo implode(", ", array_merge(["Ana", "Ben"], ["Carla"])), "\n";
echo implode(", ", array_unique(["Rizal", "Luna", "Rizal"])), "\n";

Output

Pandesal, Kape, Itlog
42
95, 90, 88
Ana, Ben, Carla
Rizal, Luna

array_column pulls one field out of a list of records, which is handy with database results.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does sort($arr) return?
2. Which keeps keys while sorting by value?
3. What does array_filter($g, fn($x) => $x >= 75) do?
4. Which function extracts the "name" field from a list of records?

By Sir Reyel · Last updated 2026-12-04