I have following function
function getSum($array){
if(is_array($array)) {
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
$sum = 0;
foreach ($iterator as $key => $value) {
$sum += $value;
}
} else{
$sum = $array;
}
return $sum;
}
And I'm using it like this
$teams = array();
$teams[1]['AREA I']['blue'] = 30;
$teams[1]['AREA I']['green'] = 25;
$teams[1]['AREA II']['blue'] = 15;
$teams[2]['AREA I']['blue'] = 40;
echo getSum($teams); //output: 110
echo getSum($teams[1]); //output: 70
echo getSum($teams[1]['AREA I']); //output: 55
echo getSum($teams[1]['AREA I']['blue']); //output: 30
How to avoid error Undefined offset when using like getSum($teams[2]['AREA IV']
(key AREA IV not set)? In this case I want that function returns zero.