$data = array:6 [
0 => array:3 [
"id" => 0
"value" => "全部"
"coupon_count" => 3
]
1 => array:3 [
"id" => 1
"name" => "客房"
"coupon_count" => 1
]
2 => array:3 [
"id" => 2
"name" => "商城"
"coupon_count" => 0
]
3 => array:3 [
"id" => 3
"name" => "电子券"
"coupon_count" => 1
]
4 => array:3 [
"id" => 4
"name" => "餐饮"
"coupon_count" => 0
]
5 => array:3 [
"id" => 5
"value" => "全场通用"
"coupon_count" => 1
]
]
//调用方法
arrayFieldSum($data, 'coupon_count');
/**
* 计算二维数组中某一元素之和
* @param array $data 二维数组
* @param string $field 需要统计的字段
* @return float|int
*/
function arrayFieldSum(array $data, string $field)
{
return array_sum(array_map(function ($val) use ($field) {
return $val[$field];
}, $data));
}
标签: php