I was surprised not to find an answer to this on SO (or elsewhere on the internet for that matter). It concerns a nested indented list which I want to convert into a multidimensional array according to the level of indentation.
By way of an example, here is some sample input:
Home
Products
Product 1
Product 1 Images
Product 2
Product 2 Images
Where to Buy
About Us
Meet the Team
Careers
Contact Us
Ideally I’d like to feed this into some (recursive?) function and get the following output:
array(
'Home' => array(),
'Products' => array(
'Product 1' => array(
'Product 1 Images' => array(),
),
'Product 2' => array(
'Product 2 Images' => array(),
),
'Where to Buy' => array(),
),
'About Us' => array(
'Meet the Team' => array(),
'Careers' => array(),
),
'Contact Us' => array(),
);
I’m confused by the logic required to perform such a task, so any help would be appreciated.
As it’s still unclear if you’re trying to read from some given structure (html-dom) or from the given string as plain text, I assumed it’s the string you’re trying to parse. If so, try:
Demo: http://codepad.org/zgfHvkBV