Link to home
Start Free TrialLog in
Avatar of webexpectations
webexpectations

asked on

php multidimensional arrays

hi,

i have a 3 dimensional array which stores categories, sub categories and sub-sub categories.

I am having diddiculty outputting the sub sub categories.

I am using a foreach loop as such:

foreach( $category_subcategory_array as $category =>$subcategorys )
        {



            echo '  <li><b>'.$category."</b>\n";

              foreach ( $subcategorys as $subcategory )
              {
                    echo '</br>-'.$subcategory."\n";
            }


        }


this is fine displaying the subcategories. how can i display the sub sub categories??

also how do i display the category, if there are no sub categories i.e can you put no data in the 2nd and 3rd arrays

thanks
Avatar of huji
huji
Flag of United States of America image

Without knowing the structure of your array I can not give a solution, but I guess you find the answer here:
foreach( $category_subcategory_array as $category =>$subcategorys )
       {
          echo '  <li><b>'.$category."</b>\n";
            foreach ( $subcategorys=>$subcategory )  /*****You missed on => I guess******/
            {
                 echo '</br>-'.$subcategory."\n";
          }
       }

Wish I can help
Huji
Avatar of WoodyRoundUp
WoodyRoundUp

is your array like this:

$subcategory["sub_cat_1"] = array ("motor","bike","bicycle");
$subcategory["sub_cat_2"] = array ("car","truck","tractor");
$subcategory1["sub_cat_3"] = array (1,2,3);
$subcategory1["sub_cat_4"] = array (3,4,5);
$category = array ("category1"->$subcategory, "category2"=>$subcategory1);

foreach ($category as $cat => $subcategory)
{
        echo "<li><b>$cat</b>\n";
        foreach ($subcategory as $subcat => $subsubcategory)
        {
                echo '</br>-'.$subcat."\n";
                foreach ($subsubcategory as $subsubcat=>$value)
               {
                        echo '</br>--'.$value."\n";
               }
        }
       echo "</li>\n";
}


the output of this should be:
<li>category1
-sub_cat_1
--motor
--bike
--bicycle
-sub_cat_2
--car
--truck
--tractor
</li>
<li>category2
-sub_cat_3
--1
--2
--3
-sub_cat_4
--3
--4
--5
</li>
Avatar of webexpectations

ASKER

it is a bit like that,

it is a an array made of 3 dimensions - $array[category][subcategories][sub-subcategories]

so, each category has many subcategories and in turn each subcategory has many sub sub categories.

i'm just wondering if you know how to output the sub sub categories?

also, if a category had no subcategories and hence no sub sub categories how would i leave the subcategories array and sub sub categories array blank?? can this be done??

thanks
yup.
can u post sample data of yours?
Can you do:

echo "<pre>"; print_r($category_subcategory_array); echo "</pre>";

and post the results, this will give a better idea of the exact structure of the array. If this will produce a great big long output (e.g. you have alot of categories and subcategories) then try limited the amount of data put into the array before your do the print_r.
here are the results :

Array
(
    [range] => Array
        (
            [0] => range-debossed.html

            [1] => range-embossed.html

            [2] => range-printed.html

        )

    [cat] => Array
        (
            [0] => cat-cat.html

            [1] => cat-cat2.html

        )

    [cat2] => Array
        (
            [0] => cat2-subcat2.html

            [cat2-subcat2.html
] => Array
                (
                    [0] => cat2-cat2subcat2-subsubcat.html

                )

        )

)

thanks
if your variable is: $array
then your code will be:

foreach ($array as $cat => $subcat_group)
{
     echo "<li><b>$cat</b>\n";      
        foreach ($subcat_group as $subcat=>$subsubcat_group)
       {
             if (is_numeric($subcat)) {
                   echo '</br>-'.$subsubcat_group."\n";
             } else {
                     echo '</br>--'.$subsubcat_group."\n";
             }
      }
      echo "</li>\n";
}

this is based on your data group.
that doesnt seem to access the sub sub category

dont you need another for loop?

if, an array was empty would that cause error? is there any way of allowing an empty array.
so for example some sub categories wont have any sub subcategories and therefore no subsubcategory array.

However, the for loop will still be there to try and access the values
Yup. It caters for that.
Did you try it?

If it fails, can you post about 2 sets of your data?
Your array structure is unique, do you know that? :p
range
-range-debossed.html
-range-embossed.html
-range-printed.html
cat
-cat-cat.html
-cat-cat2.html
cat2
-cat2-subcat2.html
--Array

is the output. I guess its just not accessing the values of the subsubcategories
ASKER CERTIFIED SOLUTION
Avatar of WoodyRoundUp
WoodyRoundUp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
excellent
thank you very much
Glad I could help.