Skip to content
Justice Tech School

Multidimensional Arrays

Arrays can contain arrays. Loop with nested foreach, extract a column with array_column and group data by building arrays.

Example
$s = [["Ada", 90], ["Tunde", 72]];
foreach ($s as [$name, $score]) echo "$name: $score\n";
$rows = [["id" => 1, "n" => "a"], ["id" => 2, "n" => "b"]];
print_r(array_column($rows, "n", "id"));
echo count($s), " ", count($s, COUNT_RECURSIVE), "\n";

Result

Ada: 90
Tunde: 72
Array
(
    [1] => a
    [2] => b
)
2 6

Exercise

Extract one column.

($rows, "n")

Quiz Test

What does array_column do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.