I have data that looks like this:
1 Root Catalog
1/2 Main Website
1/2/4 Cold Beverages
1/2/4/19 Pop - Canned
1/2/4/20 Pop - Natural Low Calorie
1/2/4/21 Pop - Bottled - Large Plastic
1/2/4/22 Pop - Bottled - Small Plastic
And need to turn it into an array that looks like:
array(
1 => array(
'name' => 'Root Catalog',
2 => array(
'name' => 'Main Website',
4 => array(
'name' => 'Cold Beverages',
19 => array(
'name' => 'Pop - Canned'
)
/*more numbers here*/
)
)
)
)
I can't figure out how to assign the array to the parent array while maintaining data integrity. The issue I'm encountering is that I can have between 1-n levels nested. I've tried exploding the string and reconstructing it using recursion, but it never turns out right. Any idea's I could try?
edit: Best Attempt so far:
foreach ($categories as $category) {
$paths = explode('/', $category->getPath());
$paths = array_reverse($paths);
$temp = array('name' => $category->getName());
foreach ($paths as $key => $value) {
$temp = array($value => $temp);
}
$data = array_merge($data, $temp);
}