Answer the question
In order to leave comments, you need to log in
How to structure category data when parsing?
Good evening. There was a difficulty in parsing categories. I'll get straight to the point.
There is something like this array with links:
Each line is a separate element of the array
/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"
]
}
}
Answer the question
In order to leave comments, you need to log in
Neanderthal solution::
$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>');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question