PHP
2
Вклад в тег
$arr = [
'/category0/subcat1/subcat2',
'/category0/subcat1/subcat4',
'/category0/subcat5/subcat6',
'/category2/subcat7/subcat8',
'/category2/subcat9/subcat10',
'/category2/subcat11/subcat12',
];
$t = [];
foreach ($arr as $item) {
$s = explode('/', $item);
$i = $s[1];
unset ($s[1], $s[0]);
if (!$t[$i] ){
$t[$i] = [];
}
foreach ($s as $item){
array_push ( $t[$i],$item);
}
}
foreach ($t as &$item){
$item = array_unique($item);
}
echo('<pre>');
var_dump($t);
var_dump(json_encode($t));
echo('<pre>');