Skip to content
Justice Tech School

Practice: Simple Cart

Use arrays of arrays and array_sum with array_column.

Example
$cart = [["n" => "pen", "p" => 2.5, "q" => 4], ["n" => "book", "p" => 10, "q" => 1]];
$total = 0;
foreach ($cart as $i) $total += $i["p"] * $i["q"];
echo number_format($total, 2), "\n";
print_r(array_column($cart, "n"));

Result

20.00
Array
(
    [0] => pen
    [1] => book
)

Exercise

Pick a column.

array__($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.