I need help with an array conversion. I have a flat array that looks like this:
Array
(
[0] => av_one_third
[1] => av_icon_box
[2] => /av_icon_box
[3] => av_button
[4] => av_icon_box
[5] => /av_icon_box
[6] => /av_one_third
)
The values of this array are actually tags from a xml like structure. what I now need is to convert this array into a nested array that resembles the following structure:
[0] => Array
(
[tag] => av_one_third
[content] => Array
(
[1] => Array
(
[tag] => av_icon_box
[content] => Array
(
)
)
[2] => Array
(
[tag] => av_button
[content] => Array
(
)
)
[3] => Array
(
[tag] => av_icon_box
[content] => Array
(
)
)
)
)
etc
Is there an easy way to do this? My first Idea was to convert the array to an xml string and use one of phps native XML functions but the problem is that self closing tags are not labeled as such. In the case above the fact that the av_button tag has no closing tag throws of the xml parsing functions i tried.
Some additional requirements: - elements can hold any number of children - final array must maintain correct order
Are there any smart array sorting functions that could easily solve this? Would appreciate any hints on this!
Best regards :)