/category0/subcat1/subcat2
/category0/subcat1/subcat4
/category0/subcat5/subcat6
/category2/subcat7/subcat8
/category2/subcat9/subcat10
/category2/subcat11/subcat12
....
/categoryN1/subcatN2/subcatN3
array(
"category0"=>array(
"subcat1"=>array("subcat2","subcat4"),
"subcat5"=>array("subcat6")
),
);
//И так далее...
{
"category0":{
"subcat1":[
"subcat2",
"subcat4"
],
"subcat5":[
"subcat6"
]
}
}
$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>');
$arr = array(
'/category0/subcat1/subcat2',
'/category0/subcat1/subcat4',
'/category0/subcat5/subcat6',
'/category2/subcat7/subcat8',
'/category2/subcat9/subcat10',
'/category2/subcat11/subcat12'
);
foreach ($arr as $key => $value) {
preg_match_all('#/category(\d+)/subcat(\d+)/subcat(\d+)#', $value, $newmatch);
$cats[] = array('category' => $newmatch[1][0], 'subcat' => $newmatch[2][0], 'subcat_' => $newmatch[3][0]);
unset($newmatch);
}
print_r($cats);
Array
(
[0] => Array
(
[category] => 0
[subcat] => 1
[subcat_] => 2
)
[1] => Array
(
[category] => 0
[subcat] => 1
[subcat_] => 4
)
[2] => Array
(
[category] => 0
[subcat] => 5
[subcat_] => 6
)
[3] => Array
(
[category] => 2
[subcat] => 7
[subcat_] => 8
)
[4] => Array
(
[category] => 2
[subcat] => 9
[subcat_] => 10
)
[5] => Array
(
[category] => 2
[subcat] => 11
[subcat_] => 12
)
)
справочник категорий (id,name,level,..)
== 1-M ==
V
категории объектов(id_категория,id_объекта)
^
== M-1 ==
объекты (id,...)