Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Looping through array in foreach

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
cditty
Smarty Rookie


Joined: 13 May 2003
Posts: 15

PostPosted: Mon Feb 16, 2004 10:51 pm    Post subject: Looping through array in foreach Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Feb 16, 2004 11:50 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP