I have a function below that is called and passed an array. I would also like to pass it a field (or key) name so that it is reusable. When I try to pass $field in the callback as below I get an error that $field is not defined. If I use a literal, it works great.
function array_map_recursive($array,$field) {
$sum = 0;
foreach ($array as $key => $value) {
foreach ($value as $key2 => $value2) {
$sum += array_sum(array_map(function($item){return $item[$field];},$value[$key2]));
}
}
return $sum;
}
called:
number_format(array_map_recursive($dept_catg_grp_descs,"total_ty_yest_sales"))