如何对多维关联数组的值求和?
我的数组如下:
$testarray=[];
$testarray[]=[
"2019-12-31" => [
"category1" => [
"total" => "10"
],
"category2" => [
"total" => "20"
],
],
"2020-01-31" => [
"category1" => [
"total" => "100"
],
"category2" => [
"total" => "200"
],
],
"2020-02-28" => [
"category1" => [
"total" => "1000"
],
"category2" => [
"total" => "2000"
]
],
];
我尝试了以下方法:
foreach($testarray[0] as $Offset=>$ArrayOfResults){
foreach($ArrayOfResults as $ResultOffset=>$Result){
$total+= $Result["total"];
}
$sums[$Offset]=$total;
}
结果是:
"2019-12-31" => 30
"2020-01-31" => 330
"2020-02-28" => 3330
如何获得所需的结果,如下将类别 1 和类别 2 值相加:
"2019-12-31" => 30
"2020-01-31" => 300
"2020-02-28" => 3000
猛跑小猪
慕娘9325324