<?php
$array = [['user1' => 20], ['user2' => 30], 3 => ['user3' => 10]];
echo array_reduce(
$array,
fn($result, $row) => $result + current($row),
0
);
echo "\n---\n";
array_walk_recursive($array, function($v)use(&$sum){ $sum += $v; }); // visit each leafnode, increase the $sum tally
echo $sum;
echo "\n---\n";
$total = 0; // without this declaration, you will generate: "Notice: Undefined variable"
foreach ($array as $subarray) {
foreach ($subarray as $v) {
$total += $v;
}
}
echo $total;
- Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- 60
---
60
---
60
preferences:
97.15 ms | 402 KiB | 123 Q