|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
cditty Smarty Rookie
Joined: 13 May 2003 Posts: 15
|
Posted: Mon Feb 16, 2004 10:51 pm Post subject: Looping through array in foreach |
|
|
I have an array that I am looping through to display my information via a foreach. I have another array that I need to match up with the records of the first array.
Can anyone offer any advice on how to reference a array at a specific location in a loop? In php, I would do a foreach and then reference the array with a incremental counter. However, I have been unsuccessfull in duplicating this in smarty.
Thanks
Chris |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Feb 16, 2004 11:50 pm Post subject: |
|
|
If the two arrays have the same keys, then it is no issue:
Code: | {foreach from=$arr1 item=i key=k}
{$arr2.$k} {*assume like keys*}
{/foreach} |
will access the related item from $arr2 as $arr1 is iterated.
If the second array must be accessed based on the ordinal given by the iteration of the first array, then you can use the iteration counter as follows (see: http://smarty.php.net/manual/en/language.function.foreach.php)
Code: | {foreach from=$arr1 item=i key=k name=test}
{assign var=temp value=$smarty.foreach.test.iteration}
{$arr2[$temp]}
{/foreach} |
Here you must name the foreach loop so that Smarty can keep track of its properties in the builtin $smarty.foreach collection.
There are other methods, of course. If this doesn't solve it for you, perhaps describe how you are storing and relating your arrays?
HTH (but its been awhile since I posted some help, so I might be rusty) |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|