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_arra y 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
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_arra
{
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
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>
$subcategory["sub_cat_1"] = array ("motor","bike","bicycle")
$subcategory["sub_cat_2"] = array ("car","truck","tractor");
$subcategory1["sub_cat_3"]
$subcategory1["sub_cat_4"]
$category = array ("category1"->$subcategory
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>
ASKER
it is a bit like that,
it is a an array made of 3 dimensions - $array[category][subcatego ries][sub- subcategor ies]
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
it is a an array made of 3 dimensions - $array[category][subcatego
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 u post sample data of yours?
Can you do:
echo "<pre>"; print_r($category_subcateg ory_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.
echo "<pre>"; print_r($category_subcateg
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.
ASKER
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
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
)
)
)
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.
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.
} else {
echo '</br>--'.$subsubcat_group
}
}
echo "</li>\n";
}
this is based on your data group.
ASKER
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
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
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
ASKER
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
-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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
excellent
thank you very much
thank you very much
Glad I could help.
foreach( $category_subcategory_arra
{
echo ' <li><b>'.$category."</b>\n
foreach ( $subcategorys=>$subcategor
{
echo '</br>-'.$subcategory."\n"
}
}
Wish I can help
Huji