nested foreach loop - how do I specify the previous foreach key as the key to multidimensional array in another foreach
| From: | Robert V. Zwink | Date: | Thu, 11 Apr 2002 11:43:33 +0000 |
| Subject: | nested foreach loop - how do I specify the previous foreach key as the key to multidimensional array in another foreach | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-3616@lists.php.net to get a copy of this message | ||
Thanks for a great product. So far I am very happy with the performance of
Smarty, I appreciate the work that has gone into this project.
I have a question about the foreach consruct. I'm using a nested foreach
loop to traverse a 2 multidimensional array. He is an example:
{foreach name=categories_left item=category_title key=category_id
from=$categories_left}
{$category_id} {$category_title}
{foreach name=subcategories_left item=subcategory_array
key=subcat_coordinate_group from=$subcategories_left}
{* check if this subcategory is a part of the current category *}
{if $category_id eq $subcat_coordinate_group}
{* now iterate through the current subcategories *}
{foreach name=subcategory item=subcategory_title key=subcategory_id
from=$subcategory_array}
{$subcategory_id} {$subcategory_title}
{/foreach}
{/if}
{/foreach}
{/foreach}
The idea is that I have two arrays, one array contains a list of categories,
the other array contains a list of subcategories. The category array is
setup like this
array("3digitID"=>"title")
and the subcategory array is setup like this
array("3digitID"=>array("4digitID"=>"title"))
This way I can use Smarty to traverse the category array, then compare the
current 3digitID to the first element in subcategory array, and display the
subcategories. Its seems like I could by pass the {if} conditional if it
were possible to specify which element of a multidimensional array to
foreach.
My question is how do I specify an element of a multidimensional array to
foreach? I want to be able to do something like this:
{foreach name=subcategories_left item=subcategory
key=subcat_coordinate_group from=$subcategories_left[$other_foreach_var]}
Where $other_foreach_var == the "key" from the current parent foreach, and
$subcategories_left is a multidimensional array.
I hope this makes sense, thanks for any help!
Robert Zwink
http://www.zwink.net/daid.php