2012-05-08 1 views
0

У меня есть массив в нижней части этого сообщения. Я использую CodeIgniter и этот массив для отображения heirarchical информации по следующей точке зрения:CodeIgniter heirarchical loop с многомерным массивом

<?php foreach($products['top_cats'] as $top_cat): // $top_cat ?> 
    <h2><?php echo $top_cat['cat_name']; ?></h2> 
    <?php if(! empty($products['sub_cats'][$top_cat['cat_id']])): ?> 
     <?php foreach($products['sub_cats'][$top_cat['cat_id']] as $sub_cat): ?> 
      <h3><?php echo $sub_cat['name']; ?></h3> 
      <!-- problem below --> 
      <?php $this->load->view('products_table_v', $products[$sub_cat['id']][$n]); ?> 
      <?php var_dump($n); ?> 
     <?php endforeach; ?> 
    <?php endif; ?> 
<?php endforeach; ?> 

$products[top_cats][14] относится к $products[sub_cats][14] и $products[sub_cats][14] относится к $products[products][14] (я надеюсь, что это имеет смысл). Поэтому мне нужно передать $products[products][14] на вид, когда я нахожусь в foreach для $products[sub_cats][14] (опять же, я надеюсь, что это имеет смысл?) ...

Моя проблема в том, что мне нужно передать правильный суб-суб (-sub) к подзапуску (который содержит хороший рабочий цикл foreach). Я не могу понять, как передать массив подзаголовку (я пошел слепой код).

Пожалуйста, помогите.

Array 
(
    [top_cats] =&gt; Array 
     (
      [14] =&gt; Array 
       (
        [cat_id] =&gt; 14 
        [cat_name] =&gt; Alcopops 
        [cat_desc] =&gt; 
        [cat_featured] =&gt; n 
        [cat_parent_id] =&gt; 
        [cat_sort_order] =&gt; 
        [category_active] =&gt; y 
       ) 

      [2] =&gt; Array 
       (
        [cat_id] =&gt; 2 
        [cat_name] =&gt; Beers and Lagers 
        [cat_desc] =&gt; <p>hh</p> 
        [cat_featured] =&gt; n 
        [cat_parent_id] =&gt; 0 
        [cat_sort_order] =&gt; 
        [category_active] =&gt; y 
       ) 

      [79] =&gt; Array 
       (
        [cat_id] =&gt; 79 
        [cat_name] =&gt; Household 
        [cat_desc] =&gt; 
        [cat_featured] =&gt; n 
        [cat_parent_id] =&gt; 
        [cat_sort_order] =&gt; 
        [category_active] =&gt; y 
       ) 

      [5] =&gt; Array 
       (
        [cat_id] =&gt; 5 
        [cat_name] =&gt; Soft Drinks 
        [cat_desc] =&gt; 
        [cat_featured] =&gt; n 
        [cat_parent_id] =&gt; 
        [cat_sort_order] =&gt; 
        [category_active] =&gt; y 
       ) 

      [4] =&gt; Array 
       (
        [cat_id] =&gt; 4 
        [cat_name] =&gt; Spirits 
        [cat_desc] =&gt; 
        [cat_featured] =&gt; n 
        [cat_parent_id] =&gt; 
        [cat_sort_order] =&gt; 
        [category_active] =&gt; y 
       ) 

      [3] =&gt; Array 
       (
        [cat_id] =&gt; 3 
        [cat_name] =&gt; Wines 
        [cat_desc] =&gt; 
        [cat_featured] =&gt; n 
        [cat_parent_id] =&gt; 
        [cat_sort_order] =&gt; 
        [category_active] =&gt; y 
       ) 

     ) 

    [sub_cats] =&gt; Array 
     (
      [3] =&gt; Array 
       (
        [25] =&gt; Array 
         (
          [id] =&gt; 25 
          [name] =&gt; Champagne and Sparkling Wines 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 3 
         ) 

        [22] =&gt; Array 
         (
          [id] =&gt; 22 
          [name] =&gt; Australian Wines 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 3 
         ) 


       ) 

      [14] =&gt; Array 
       (
        [30] =&gt; Array 
         (
          [id] =&gt; 30 
          [name] =&gt; White Rum 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 4 
         ) 

        [32] =&gt; Array 
         (
          [id] =&gt; 32 
          [name] =&gt; Liquers 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 4 
         ) 

       ) 

      [2] =&gt; Array 
       (
        [8] =&gt; Array 
         (
          [id] =&gt; 8 
          [name] =&gt; Canned Lager 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 2 
         ) 

        [12] =&gt; Array 
         (
          [id] =&gt; 12 
          [name] =&gt; Bottled Beer 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 2 
         ) 

        [10] =&gt; Array 
         (
          [id] =&gt; 10 
          [name] =&gt; Bitter Cans 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 2 
         ) 

        [7] =&gt; Array 
         (
          [id] =&gt; 7 
          [name] =&gt; Super Beers 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 2 
         ) 

        [13] =&gt; Array 
         (
          [id] =&gt; 13 
          [name] =&gt; Cider 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 2 
         ) 

        [9] =&gt; Array 
         (
          [id] =&gt; 9 
          [name] =&gt; Stout 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 2 
         ) 

       ) 

      [] =&gt; Array 
       (
        [5] =&gt; Array 
         (
          [id] =&gt; 5 
          [name] =&gt; Soft Drinks 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 
         ) 

        [79] =&gt; Array 
         (
          [id] =&gt; 79 
          [name] =&gt; Household 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 
         ) 

        [14] =&gt; Array 
         (
          [id] =&gt; 14 
          [name] =&gt; Alcopops 
          [desc] =&gt; 
          [cat_parent_id] =&gt; 
         ) 

       ) 

     ) 

    [products] =&gt; Array 
     (
      [25] =&gt; Array 
       (
        [1347] =&gt; Array 
         (
          [product_id] =&gt; 1347 
          [category_id] =&gt; 25 
          [name] =&gt; Asti Spumante Martini 
          [description] =&gt; 
          [source_price] =&gt; 29.00 
          [source_pack_size] =&gt; 6 
          [source_item_size] =&gt; 75.00 
          [source_unit_id] =&gt; 2 
          [resale_price] =&gt; 7.00 
          [resale_pack_size] =&gt; 1 
          [percentage_return] =&gt; 22.59 
          [product_code] =&gt; WNAS001 
          [product_image] =&gt; 1347_.jpg 
          [current_stock] =&gt; 5 
          [stock_level_id] =&gt; 0 
          [featured] =&gt; y 
          [active] =&gt; y 
          [so_member_of_group_id] =&gt; 0 
          [discount_amount] =&gt; 3.50 
          [created_at] =&gt; 2009-07-22 10:21:57 
          [modified_at] =&gt; 
          [product_unit_id] =&gt; 2 
          [unit_name] =&gt; Centilitre 
          [unit_symbol] =&gt; cl 
          [stock_levels_id] =&gt; 
          [stock_levels_name] =&gt; 
          [cat_id] =&gt; 25 
          [cat_name] =&gt; Champagne and Sparkling Wines 
          [cat_desc] =&gt; 
          [cat_featured] =&gt; n 
          [cat_parent_id] =&gt; 3 
          [cat_sort_order] =&gt; 
          [category_active] =&gt; y 
         ) 

        [1566] =&gt; Array 
         (
          [product_id] =&gt; 1566 
          [category_id] =&gt; 25 
          [name] =&gt; Blossom Hill Zinfandel Sparkling 
          [description] =&gt; 
          [source_price] =&gt; 29.99 
          [source_pack_size] =&gt; 6 
          [source_item_size] =&gt; 70.00 
          [source_unit_id] =&gt; 2 
          [resale_price] =&gt; 8.49 
          [resale_pack_size] =&gt; 1 
          [percentage_return] =&gt; 29.35 
          [product_code] =&gt; WNBH010 
          [product_image] =&gt; 
          [current_stock] =&gt; 30 
          [stock_level_id] =&gt; 
          [featured] =&gt; n 
          [active] =&gt; y 
          [so_member_of_group_id] =&gt; 
          [discount_amount] =&gt; 0.00 
          [created_at] =&gt; 2011-03-21 10:35:47 
          [modified_at] =&gt; 
          [product_unit_id] =&gt; 2 
          [unit_name] =&gt; Centilitre 
          [unit_symbol] =&gt; cl 
          [stock_levels_id] =&gt; 
          [stock_levels_name] =&gt; 
          [cat_id] =&gt; 25 
          [cat_name] =&gt; Champagne and Sparkling Wines 
          [cat_desc] =&gt; 
          [cat_featured] =&gt; n 
          [cat_parent_id] =&gt; 3 
          [cat_sort_order] =&gt; 
          [category_active] =&gt; y 
         ) 


       ) 

      [22] =&gt; Array 
       (
        [1020] =&gt; Array 
         (
          [product_id] =&gt; 1020 
          [category_id] =&gt; 22 
          [name] =&gt; Auction House Cabernet Sauvignon Merlot (Aus) 
          [description] =&gt; 
          [source_price] =&gt; 20.00 
          [source_pack_size] =&gt; 6 
          [source_item_size] =&gt; 75.00 
          [source_unit_id] =&gt; 2 
          [resale_price] =&gt; 5.00 
          [resale_pack_size] =&gt; 1 
          [percentage_return] =&gt; 27.18 
          [product_code] =&gt; WNAH008 
          [product_image] =&gt; 1020_.jpg 
          [current_stock] =&gt; 35 
          [stock_level_id] =&gt; 0 
          [featured] =&gt; y 
          [active] =&gt; y 
          [so_member_of_group_id] =&gt; 0 
          [discount_amount] =&gt; 0.75 
          [created_at] =&gt; 2008-03-10 14:56:16 
          [modified_at] =&gt; 
          [product_unit_id] =&gt; 2 
          [unit_name] =&gt; Centilitre 
          [unit_symbol] =&gt; cl 
          [stock_levels_id] =&gt; 
          [stock_levels_name] =&gt; 
          [cat_id] =&gt; 22 
          [cat_name] =&gt; Australian Wines 
          [cat_desc] =&gt; 
          [cat_featured] =&gt; n 
          [cat_parent_id] =&gt; 3 
          [cat_sort_order] =&gt; 
          [category_active] =&gt; y 
         ) 

       ) 

     ) 

) 
+0

В вашем примере вы не имеете продукты к идентификатору 14. –

+0

К сожалению, игнорируют, что (есть один в реальном массиве - мне пришлось обрезать его вниз значительно, чтобы показать вам, как это было 15000+ строк). :) – atwright147

ответ

1
foreach($products['top_cats'] as $top_cat): // $top_cat ?> 
    <h2><?php echo $top_cat['cat_name']; ?></h2> 
    <?php if(! empty($products['sub_cats'][$top_cat['cat_id']])): ?> 
     <?php foreach($products['sub_cats'][$top_cat['cat_id']] as $sub_cat): ?> 
      <h3><?php echo $sub_cat['name']; ?></h3> 

      // Replaced bit 
      <?php if(! empty($products['products'][$sub_cat['id']])): ?> 
       <?php $this->load->view('products_table_v', $products['products'][$sub_cat['id']]); ?> 
      <?php endif; ?> 

     <?php endforeach; ?> 
    <?php endif; ?> 
<?php endforeach; ?> 
+0

Удивительный! Я попробую это сегодня вечером, но это выглядит хорошо (и невероятно просто - у меня была огромная заморозка мозгов по этому поводу) :) – atwright147

+0

:) Это был также довольно сложный способ пройти через массив со всем пролетом id :) , Было бы проще, если бы он просто перешел $ topcategories ['topcategory1'] ['subcategories] [' subcategory1 '] [' products '] [' product1]. Сделал бы foreach гнездом много simpeler :). – Zombaya

+0

Мне не нравится делать что-то легким путем. :) – atwright147

Смежные вопросы