我有一个数组($categories_final),看起来类似于:
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
)
我试图让它看起来像这样,所以我可以将它作为我商店的类别结构添加到数据库中:
Array
(
[Accessories] => Array
(
[0] => Apron
[1] => Banners
[2] => Belts
)
[Apparel] => Array
(
[0] => Men => Array
(
[0] => Belts
[1] => Socks
)
[1] => Women => Array
(
[0] => Leggings
)
)
...etc
这是我到目前为止所得到的,但我无法弄清楚如何在 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);
再一次,我坚持如何在他们各自的父母下添加 /Belts、/Socks 和 /Leggings。
任何帮助表示赞赏,谢谢!
慕容3067478
慕丝7291255
温温酱