I'm trying to figure out how to restructure an array and total values by given keys. I currently have an array called $data that is returning this result:
Bill => array:3 [▼
"01" => array:6 [▼
"Food " => array:2 [▶]
hamburger=>array:[
sales=> 210.00
purchases=>200.00
]
burrito=>array:[
sales=> 100.00
purchases=>40.00
]
"Drink " => array:2 [▶]
coke=>array:[
sales=> 210.00
purchases=>200.00
]
pepsi=>array:[
sales=> 100.00
purchases=>40.00
]
"total" => array:7 [▶]
sales=>620.00
purchases=>480.00
]
]
Ted => array:3 [▼
"01" => array:6 [▼
"Food " => array:2 [▶]
hamburger=>array:[
sales=> 110.00
purchases=>100.00
]
burrito=>array:[
sales=> 120.00
purchases=>40.00
]
"Drink " => array:2 [▶]
coke=>array:[
sales=> 110.00
purchases=>100.00
]
pepsi=>array:[
sales=> 120.00
purchases=>40.00
]
"total" => array:7 [▶]
sales=>460.00
purchases=>280.00
]
]
My issue is that I need to fit this into a laravel blade that is not concerned with bill or ted, but rather totals for everyone. Where the existing keys are "Bill" and "Ted", I would need the new structure to actually be identified by the "01", but I want to total all of the categories within that level. So I would be hoping to get results like this:
"01" =>[▼
"Food " => array:2 [▶]
hamburger=>array:[
sales=> 320.00
purchases=>300.00
]
burrito=>array:[
sales=> 220.00
purchases=>140.00
]
"Drink " => array:2 [▶]
coke=>array:[
sales=> 320.00
purchases=>300.00
]
pepsi=>array:[
sales=> 220.00
purchases=>80.00
]
"total" => array:7 [▶]
sales=>1080.00
purchases=>760.00
]
I can easily loop on this like:
foreach($data as $key=> $value){
foreach($value as $categoryNumber => $categories){
foreach($categories as $categoryDetails => $details){
}
}
}
But I'm completely stuck as to how to re-structure this and total by the new indices