Please refer to the solution, this has been solved.
I have the following dataset and no ids are provided, trying to get this completed using recursion.
Should I attempt this or should I go another route? Because there are no ids.
After a filter on each attribute the root is gender, node 1 is category and end node is label. I have tried to use array_merge_recursive, array_push and I have tried to construct my own recursive pattern but nothing appears to get the pattern I want.
JSON data:
[{"label":"Shirts","tag":"M_SHIRT","gender":"Men","category":"Clothing"},
{"label":"Pants","tag":"M_PANT","gender":"Men","category":"Clothing"},
{"label":"Shorts","tag":"M_SHORT","gender":"Men","category":"Clothing"},
{"label":"Casual","tag":"M_SHOE_CASUAL","gender":"Men","category":"Shoes"},
{"label":"Tennis","tag":"M_SHOE_TENNIS","gender":"Men","category":"Shoes"},
{"label":"Watches","tag":"M_ACCESS_WATCH","gender":"Men","category":"Accessories"},
{"label":"Belts","tag":"M_ACCESS_BELT","gender":"Men","category":"Accessories"},
{"label":"Ties","tag":"M_ACCESS_TIE","gender":"Men","category":"Accessories"}]
Men
->Accessories
->Watches
->Belts
->Ties
->Clothing
->Pants
->Shirts
->Shorts
->Shoes
->Casual
->Tennis
Here is my PHP code for the recursion which is messy:
function buildSideBar($searchLayers){
try{
$sidebar;
$count = 0;
foreach($searchLayers[$count] as $root){
//$sideBarData[]=array("root"=>$root);
$sideBarData[]=$root;
$searchLayers[$count]='';
$this->addChildren($sideBarData,$searchLayers,1,$root, $count);
$count++;
}
var_dump($sideBarData);
}catch (Exception $ex){
log($ex);
}
}
function addChildren(&$sideBarData,$layers,$level,$parent,$count){
if(!empty($layers[$level]) && is_array($layers[$level])){
foreach($layers[$level] as $child){
//check if child is a node
if($this->verifyChildBelongsToParent($child,$parent)){
if($level==1)
{
$count = 0;
// $sideBarData = array_merge_recursive($sideBarData, array("root"=>array("child".$level=>$child)));
$sideBarData[0][$level] = $child;
}
else
{
$sideBarData[][][$level] = $child;
}
$parents[]=$parent;
$parents[]=$child;
//var_dump($parents);
if($level<3)
{
// $this->addChildren($sideBarData,$layers,$level++,$parents);
}
}
//
}
}
}
function verifyChildBelongsToParent($child,$parent){
//var_dump($this->categoryData);break;
foreach($this->categoryData as $category){
if(is_array($parent) && sizeof($parent)>1){
echo 'Child' . $child;
echo 'PARENTS ';
var_dump($parent);
var_dump($category);
if(strcmp($category->getGender(),$parent[0])==0 && strcmp($category->getCategory(),$parent[1])==0)
{
var_dump($child);
echo 'Add child ' . $child;
return true;
}
}
else{
//echo 'check if ' . $parent .' has child ' . $child;
if(strcmp($category->getGender(),$parent)==0)
{
// var_dump($parent);
// var_dump($child);
// echo 'Add Child';
return true;
}
}
}
return false;
}
Here is the $searchLayers data that I pass in:
Array
(
[0] => "MEN",
[1] => Array(
[0]=>'Accessories',
[1]=>'Clothing',
[2]=>'Shoes'
),
[2] =>Array(
[0]=>'Belts',
[1]=>'Casual',
[2]=>'Shirts',
[3]=>'Shorts',
[4]=>'Tennis',
[5]=>'Ties',
[6]=>'Watches'
)
);