3v4l.org

run code in 300+ PHP versions simultaneously
<?php $teams = [ 1 => [ 'AREA I' => [ 'blue' => 30, 'green' => 25, ], 'AREA II' => [ 'blue' => 15, ], ], 2 => [ 'AREA I' => [ 'blue' => 40, ], ], ]; $filterSets = [ [], [1], [2], [null, 'AREA I'], [null, 'AREA II'], [null, null, 'blue'], [null, null, 'green'], [null, 'AREA I', 'blue'], ['foo'], [null, null, null, null], ]; function sumViaPath(array $array, array $path): int { $sum = 0; $filterKey = $path ? array_shift($path) : null; foreach ($array as $k => $v) { if ($filterKey === null || $filterKey === $k) { $sum += is_array($v) ? sumViaPath($v, $path) : $v; } } return $sum; } foreach ($filterSets as $path) { $results[json_encode($path)] = sumViaPath($teams, $path); } var_export($results);

This is an error 500

Value for `_results` contains invalid data `array`


preferences:
151.57 ms | 1638 KiB | 12 Q