如何對(duì)多維關(guān)聯(lián)數(shù)組的值求和?我的數(shù)組如下:$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;}結(jié)果是:"2019-12-31" => 30"2020-01-31" => 330"2020-02-28" => 3330如何獲得所需的結(jié)果,如下將類別 1 和類別 2 值相加:"2019-12-31" => 30"2020-01-31" => 300"2020-02-28" => 3000
如何求多維關(guān)聯(lián)數(shù)組的和?
桃花長(zhǎng)相依
2024-01-19 10:48:27