3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = 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' ); $result =[]; foreach($arr as $val){ $count = count(explode("/", $val)); $str = '{"' . str_replace('/', '":{"', $val) . '"}' . str_repeat("}", $count-1); $json = preg_replace("/(.*)(\{)(.*?)(\})/", "$1[$3]", $str); $result = array_merge_recursive($result, json_decode($json, true)); } Print_r($result);

preferences:
24.51 ms | 410 KiB | 5 Q