2016-11-17 2 views
0

解決策を参照してください、これは解決されました。再帰 - IDなしでデータツリーを構築するには、データが曖昧です。

私は次のデータセットを持っていますが、これは再帰を使用してこれを取得しようとしているIDはありません。

これを試すか、別のルートに移動する必要がありますか? idsがないからです。

各属性のフィルタの後、ルートはジェンダーです。ノード1はカテゴリであり、エンドノードはラベルです。私はarray_merge_recursive、array_pushを使用しようとしましたが、私自身の再帰的なパターンを構築しようとしましたが、何も必要なパターンを得ることはできません。

JSONデータ:ここ

[{"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 

が厄介である再帰のための私のPHPコードである:ここで

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; 


    } 

は私が渡し$ searchLayersデータである:

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' 
    ) 
); 
+0

これは 'foreach'で簡単です - 再帰を使う必要がある理由はありますか? –

+0

@DarraghEnrightメニューを作成するための簡単なforeachは、ソリューションとしてコードテンプレートを提供できますか?私はそれを見たいと思います。 :) – mcv

+0

@RyanVincentはい私はそれを見た。私はこのサンプルデータが私に提供されたのは実際には地域になかったと思います。下に私のソリューションを掲載したので、私はこの問題を解決しました。私はまだ誰かが簡単なソリューションを投稿することを歓迎します。 :) – mcv

答えて

0

だから私の解決策、おそらくより良いものがありますが、私はこの3つの異なる技術を常に考えていましたuldはスケーラブルです。

デザインには次の制限があります。サイドメニューバーは3レベルしかできません。

私は最初、上記の$searchLayersで詳述されている祖父母、親子を見つけるためにデータをソートしましたが、これを親階層に従った3つの別個の配列($layer1, $layer2, $layer3)として渡しました。ここで

function buildSideMenu($layer1,$layer2,$layer3)

//starts the construction of a side menu and adds each layer of children to the parent 
function buildSideMenu($layer1, $layer2, $layer3){ 
    $sideMenu = '<ul class="nav nav-stacked">'; 
    foreach($layer1 as $layer){ 
     $sideMenu.= '<li><a class="expandable" href="#">'.$layer.'</a></li>'; 
     //adds children founded to the parent 
     $sideMenu .= $this->buildSideMenuChildern($layer,$layer2,$layer3); 

    } 
    $sideMenu .= '</ul>'; 
    $this->sideMenu = $sideMenu; 
} 

//builds on children to a side bar that belogn to a parent 
function buildSideMenuChildern($parent,$layer1,$layer2=''){ 
    $entries =''; 
    foreach($layer1 as $layer){ 
     //deter mines if the child belongs to the parent 
     if($this->isLayerParentMatch($layer,$parent)){ 
      //if this child has it's own childern, process needs to be repeated, else child is an leaf (aka end node) 
      if(!empty($layer2)){ 
       $entries .= '<li><a class="expandable" href="/'.$this->getTag($layer).'">'.$layer.'</a></li>'; 
       $entries .= $this->buildSideMenuChildern(array($parent,$layer),$layer2); 
      }else{ 
       $entries .= '<li><a href="/'.$this->getTag($layer).'">'.$layer.'</a></li>'; 
      } 
     } 
    } 
    $entries = (!empty($entries))? '<ul style="display:block;">'.$entries.'</ul>':$entries; 
    return $entries; 
} 

//populates href with tag if one is available, converts text to lower case 
function getTag($label){ 
    foreach($this->categoryData as $category){ 
     if(strcmp($label,$category->getLabel())==0) 
     { 
      return $category->getTag(); 
     } 
    } 
    return '#'. strtolower($label); 
} 

//determines if child is associated with all parents provided 
function isLayerParentMatch($child,$parent){ 
    if(empty($child) || empty($parent)) return false; 
    foreach($this->categoryData as $category){ 
     if(!is_array($parent) && strcmp($category->getGender(),$parent)==0 && strcmp($category->getCategory(),$child)==0) 
     { 
      return true; 
     }else if(strcmp($category->getGender(),$parent[0])==0 
       && strcmp($category->getCategory(),$parent[1])==0 
       && strcmp($category->getLabel(),$child)==0){ 
      return true; 
     } 
    } 
    return false; 
} 

function getSideMenu(){ 
     return $this->sideMenu; 
} 

一番上の関数を呼び出すことにより、すべての始まりは、また、私は私がのためにゲッターとセッターを持っているカテゴリーのPHPクラスにJSONデータを変換することに、注意してくださいコードであり、各属性はjsonデータで定義されています。このクラスはこのコードからも省略されています。

関連する問題