I've got an array ($categories_final), looks similar to this:
Array
(
[0] => Accessories/Apron
[1] => Accessories/Banners
[2] => Accessories/Belts
[3] => Brand/Brand1
[4] => Brand/Brand2
[5] => Apparel/Men/Belts
[6] => Apparel/Men/Socks
[7] => Apparel/Women/Leggings
)
I'm trying to get it to look like this so I can add it to a database as my store's category structure:
Array
(
[Accessories] => Array
(
[0] => Apron
[1] => Banners
[2] => Belts
)
[Apparel] => Array
(
[0] => Men => Array
(
[0] => Belts
[1] => Socks
)
[1] => Women => Array
(
[0] => Leggings
)
)
...etc
Here's what I've got so far, but I am under able to figure out how to add the children under Men/ & Women/:
$categories = array();
foreach ($categories_final as $cat) {
$levels = explode('/', $cat);
if (isset($categories[$levels[0]])) {
if (!in_array($levels[1], $categories[$levels[0]])) {
$categories[$levels[0]][]= $levels[1];
}
}
else {
$categories[$levels[0]][]= $levels[1];
}
}
print_r($categories);
Again, I'm stuck on how to add the /Belts, /Socks & /Leggings under their respective parents.
Any helps is appreciated, thanks!