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
webexpectationsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hujiCommented:
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
WoodyRoundUpCommented:
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>
webexpectationsAuthor Commented:
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
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

WoodyRoundUpCommented:
yup.
can u post sample data of yours?
ShelfieldCollegeCommented:
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.
webexpectationsAuthor Commented:
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
WoodyRoundUpCommented:
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.
webexpectationsAuthor Commented:
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
WoodyRoundUpCommented:
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
webexpectationsAuthor Commented:
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
WoodyRoundUpCommented:
OK.
This one should be fine.
Give it a go.

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 {
                   foreach ($subsubcat_group as $lastsub=>$lastsub_group)
                    {
                        echo '</br>--'.$lastsub_group."\n";
                   }
             }
      }
      echo "</li>\n";
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
webexpectationsAuthor Commented:
excellent
thank you very much
WoodyRoundUpCommented:
Glad I could help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.