2017-10-31 28 views
0

カテゴリとサブカテゴリのリストが表示されるこのコードはありますが、私はchildCategoriesの内部にある必要があります.2段階以上であれば既に追加できます。それを追加する。シナリオは次のようになります: -多次元配列の子に追加

親カテゴリは「電子」で、サブカテゴリはスマートフォン内に「スマートフォン」と「ラップトップ」、AsusとSamsungがあり、Sameはラップトップになります。ラップトップのカテゴリには、「Alienware Series」と「Vostro Series」があります。

電子

- >スマートフォン

- > Asusの

- >サムスン

- >

をラップトップ - > HP

- > Dell ---> Alienwareシリーズ、Vostroシリーズ

私は配列はこのようになりたい...ここ

Array 
     (
      [categoryId] => 3 
      [title] => Electronic 
      [bannerImage] => elect.jpg 
      [childCategories] => Array 
       (
        Array 
         (
          [categoryId] => 4 
          [title] => Smartphone 
          [bannerImage] => smartphone-banner.png 
          [childCategories] => Array 
           (
            Array 
             (
             [categoryId] => 4 
             [title] => Asus 
             [bannerImage] => 
             [childCategories] => Array 
                  (

                  ) 
            ), 

            Array 
             (
             [categoryId] => 5 
             [title] => Samsung 
             [bannerImage] => 
             [childCategories] => Array 
                  (

                  ) 
            ) 


           ), 
          Array 
          (
            [categoryId] => 6 
            [title] => Laptops 
            [bannerImage] => 
            [childCategories] => Array 
            (
             Array 
             (
             [categoryId] => 7 
             [title] => HP 
             [bannerImage] => 
             [childCategories] => Array 
                  (

                  ) 
             ), 

             Array 
             (
              [categoryId] => 8 
              [title] => Dell 
              [bannerImage] => 
              [childCategories] => Array 
                   (

                    Array 
                    (
                     [categoryId] => 9 
                     [title] => Alienware Series 
                     [bannerImage] => 
                     [childCategories] => Array 
                     (

                     ) 
                    ), 
                    Array 
                    (
                     [categoryId] => 10 
                     [title] => Vostro Series 
                     [bannerImage] => 
                     [childCategories] => Array 
                     (

                     ) 
                    ) 


                  ), 

             ) 

            ) 
          ) 
         ) 
       ) 

は私のコードです: -

if($category->getData('level') > 2) { 
    $temp_array = array(); 

    $paths = explode("/", $category->getData('path')); 
    for ($x=0; $x <= count($paths); $x++) { 

     if($x == 0 || $x == 1) { 
      unset($paths[$x]); 
     } 
     else { 
      $temp_array['categoryId'] = $category->getData('entity_id'); 
      $temp_array['title'] = $category->getData('name'); 
      $temp_array['bannerImage'] = $category->getData('image'); 
      $temp_array['childCategories'] = array(); 
      $temp_array['path'] = $category->getData('path'); 
      $temp_array['parent'] = $category->getData('parent_id'); 
      $temp_array['level'] = $category->getData('level'); 
      $category_array[$category->getData('parent_id')]['childCategories'][$category->getData('entity_id')] = $temp_array;      
     } 
    } 
} 

注将来的に追加されるより下位カテゴリーとなりますがある場合は、こと。したがって、通常の配列割り当てはできません。

更新しますJSON形式の入力データを使用して3

{ 
    "3": { 
     "categoryId": "3", 
     "title": "Electronic", 
     "bannerImage": "elect.jpg", 
     "childCategories": [], 
     "parent": "2", 
     "path": "1/2/3" 
    }, 
    "4": { 
     "categoryId": "4", 
     "title": "Smartphone", 
     "bannerImage": "smartphone-banner.png", 
     "childCategories": [], 
     "parent": "3", 
     "path": "1/2/3/4" 
    }, 
    "5": { 
     "categoryId": "5", 
     "title": "Laptop", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "3", 
     "path": "1/2/3/5" 
    }, 
    "6": { 
     "categoryId": "6", 
     "title": "Health", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "2", 
     "path": "1/2/6" 
    }, 
    "7": { 
     "categoryId": "7", 
     "title": "Vitamin", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "6", 
     "path": "1/2/6/7" 
    }, 
    "9": { 
     "categoryId": "9", 
     "title": "Fashion", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "2", 
     "path": "1/2/9" 
    }, 
    "10": { 
     "categoryId": "10", 
     "title": "For Her", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "9", 
     "path": "1/2/9/10" 
    }, 
    "11": { 
     "categoryId": "11", 
     "title": "For Him", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "9", 
     "path": "1/2/9/11" 
    }, 
    "12": { 
     "categoryId": "12", 
     "title": "Samsung", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "4", 
     "path": "1/2/3/4/12" 
    }, 
    "13": { 
     "categoryId": "13", 
     "title": "Dell", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "5", 
     "path": "1/2/3/5/13" 
    }, 
    "14": { 
     "categoryId": "14", 
     "title": "Alienware", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "13", 
     "path": "1/2/3/5/13/14" 
    }, 
    "15": { 
     "categoryId": "15", 
     "title": "Vostro", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "13", 
     "path": "1/2/3/5/13/15" 
    }, 
    "timestamp": "2017-10-31 04:20:20" 
} 
+0

は、カテゴリがどのように見えるかの単なる視覚的な図です。 – N00bMagg

+0

はい、わかりました。しかし、私があなたを手伝ってくれるようにしようとすると、ソリューションの開発とテストを行うために、サンプル入力の最小限/完全なセットを書く時間を費やす必要があります。 - これは、あなたの質問に魅力を感じさせない(ほとんどのボランティアに)。 – mickmackusa

+0

@mickmackusa注目、あなたのためのjson形式を生成します。 – N00bMagg

答えて

0

$json='{ 
    "3": { 
     "categoryId": "3", 
     "title": "Electronic", 
     "bannerImage": "elect.jpg", 
     "childCategories": [], 
     "parent": "2", 
     "path": "1/2/3" 
    }, 
    "4": { 
     "categoryId": "4", 
     "title": "Smartphone", 
     "bannerImage": "smartphone-banner.png", 
     "childCategories": [], 
     "parent": "3", 
     "path": "1/2/3/4" 
    }, 
    "5": { 
     "categoryId": "5", 
     "title": "Laptop", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "3", 
     "path": "1/2/3/5" 
    }, 
    "6": { 
     "categoryId": "6", 
     "title": "Health", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "2", 
     "path": "1/2/6" 
    }, 
    "7": { 
     "categoryId": "7", 
     "title": "Vitamin", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "6", 
     "path": "1/2/6/7" 
    }, 
    "9": { 
     "categoryId": "9", 
     "title": "Fashion", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "2", 
     "path": "1/2/9" 
    }, 
    "10": { 
     "categoryId": "10", 
     "title": "For Her", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "9", 
     "path": "1/2/9/10" 
    }, 
    "11": { 
     "categoryId": "11", 
     "title": "For Him", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "9", 
     "path": "1/2/9/11" 
    }, 
    "12": { 
     "categoryId": "12", 
     "title": "Samsung", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "4", 
     "path": "1/2/3/4/12" 
    }, 
    "13": { 
     "categoryId": "13", 
     "title": "Dell", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "5", 
     "path": "1/2/3/5/13" 
    }, 
    "14": { 
     "categoryId": "14", 
     "title": "Alienware", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "13", 
     "path": "1/2/3/5/13/14" 
    }, 
    "15": { 
     "categoryId": "15", 
     "title": "Vostro", 
     "bannerImage": null, 
     "childCategories": [], 
     "parent": "13", 
     "path": "1/2/3/5/13/15" 
    }, 
    "timestamp": "2017-10-31 04:20:20" 
}'; 

Iは、非再帰的なアプローチを使用しています。子供たちを両親に入れ子にして、最も深い子供たちを最初に育て、「底」から仕上げることは、積み重ね技法の多くです。ステップバイステップの説明については、私のインラインコメントを参照してください。

コード:(Demo

$array=json_decode($json,true); 
unset($array['timestamp']); // remove unwanted timestamp 
uasort($array,function($a,$b){return substr_count($b['path'],'/')<=>substr_count($a['path'],'/');}); // sort by depth (deepest to shallowest) 
foreach($array as $key=>&$subarray){ 
    $data=array_intersect_key($subarray,['title'=>'','bannerImage'=>'','childCategories'=>'']); // store only vital data based on nominated keys 
    if(isset($array[$subarray['parent']])){ 
     $array[$subarray['parent']]['childCategories'][$key]=$data; // insert into parent (on same level) 
     unset($array[$key]); // remove child from original "unnested" location 
    }else{ 
     $subarray=$data; // no movement needed, just trim unwanted elements from subarray 
    } 
} 
ksort($array); // resort by first level keys, ASC 
var_export($array); 

これが出力されます:矢印@mickmackusa

array (
    3 => 
    array (
    'title' => 'Electronic', 
    'bannerImage' => 'elect.jpg', 
    'childCategories' => 
    array (
     4 => 
     array (
     'title' => 'Smartphone', 
     'bannerImage' => 'smartphone-banner.png', 
     'childCategories' => 
     array (
      12 => 
      array (
      'title' => 'Samsung', 
      'bannerImage' => NULL, 
      'childCategories' => 
      array (
      ), 
     ), 
     ), 
    ), 
     5 => 
     array (
     'title' => 'Laptop', 
     'bannerImage' => NULL, 
     'childCategories' => 
     array (
      13 => 
      array (
      'title' => 'Dell', 
      'bannerImage' => NULL, 
      'childCategories' => 
      array (
       14 => 
       array (
       'title' => 'Alienware', 
       'bannerImage' => NULL, 
       'childCategories' => 
       array (
       ), 
      ), 
       15 => 
       array (
       'title' => 'Vostro', 
       'bannerImage' => NULL, 
       'childCategories' => 
       array (
       ), 
      ), 
      ), 
     ), 
     ), 
    ), 
    ), 
), 
    6 => 
    array (
    'title' => 'Health', 
    'bannerImage' => NULL, 
    'childCategories' => 
    array (
     7 => 
     array (
     'title' => 'Vitamin', 
     'bannerImage' => NULL, 
     'childCategories' => 
     array (
     ), 
    ), 
    ), 
), 
    9 => 
    array (
    'title' => 'Fashion', 
    'bannerImage' => NULL, 
    'childCategories' => 
    array (
     10 => 
     array (
     'title' => 'For Her', 
     'bannerImage' => NULL, 
     'childCategories' => 
     array (
     ), 
    ), 
     11 => 
     array (
     'title' => 'For Him', 
     'bannerImage' => NULL, 
     'childCategories' => 
     array (
     ), 
    ), 
    ), 
), 
)