I have an array:
$array = [
"main;header;up" => "main_header_up value",
"main;header;bottom" => "main_header_bottom value",
"main;bottom" => "main_bottom value",
"main;footer;right;top" => "main_footer_right_top value"
];
What I'd like to get is explode the array keys into multidimensional array but keep the values and the result should be equivalent of this array:
$array = [
"main" => [
"header" => [
"up" => "main_header_up value",
"bottom" => "main_header_bottom value"
],
"bottom" => ["main_bottom value"],
"footer" => [
"right" => [
"top" => "main_footer_right_top value
]
]
]
];
I guess I should state that the number of ; is not predetermined. There could be none or there could be 10 (or more) of them in a key / index.
Is there any elegant way to achieve this?
explodehas nothing to do here. You could write your own function to parse such input. I recommend using nested objects (i.e.main->header->upandmain->bottom..) etcjson_decode / json_encodewould do the trick though.explodeis part of the solution, but not the tricky one. Tricky is, that you'll have to write your own function to transfer those resulting arrays into a multidimentional array/object while keeping the values in the right place.