I have the following array structure from a database query: I extended my previous posts, with more details about the array structure. Hope that helps to better understand my example.
array:7 [▼
0 => array:4 [▼
0 => array:2 [▼
"my_first_key" => "option 1"
"SUMME" => "6"
]
1 => array:2 [▼
"my_first_key" => "option 2"
"SUMME" => "22"
]
2 => array:2 [▼
"my_first_key" => "option 3"
"SUMME" => "37"
]
3 => array:2 [▼
"my_first_key" => "option 4"
"SUMME" => "42"
]
],
0 => array:4 [▼
0 => array:2 [▼
"my_second_key" => "option 1"
"SUMME" => "6"
]
1 => array:2 [▼
"my_second_key" => "option 2"
"SUMME" => "22"
]
2 => array:2 [▼
"my_second_key" => "option 3"
"SUMME" => "37"
]
3 => array:2 [▼
"my_second_key" => "option 4"
"SUMME" => "42"
]
]
...
]
How can I transform it to a much simpler structure like:
array:8 [▼
"my_first_key" => array:4 [▼
"option 1" => "6"
"option 2" => "22"
"option 3" => "37"
"option 4" => "42"
],
"my_second_key" => array:4 [▼
"option 1" => "6"
"option 2" => "22"
"option 3" => "37"
"option 4" => "42"
]
...
]
I tried to give array_walk_recursive a try, but I can't grab the SUMME key and bind it to the previous option?
$tmpArr = [];
array_walk_recursive($data, static function ($value, $key) use (&$tmpArr) {
$tmpArr[$key][$value] = $value; // not $value? SUMME here?
}, $tmpArr);