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

Comparing array items in a foreach loop.

 
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
elyod_72
Smarty n00b


Joined: 11 Aug 2008
Posts: 4

PostPosted: Fri Feb 20, 2009 7:27 pm    Post subject: Comparing array items in a foreach loop. Reply with quote

I have an associative array that I am using foreach to loop thru. In deciding whether or not to display portions of a row, I want to look at the previous row's contents and only display if the contents is different.

Below is an example:
I have the following associative array:
Array(
[0] => Array(
[Player] => Dave
[Type] => Cell
[Number] => 665-123-1234
)
[1] => Array(
[Player] => Dave
[Type] = >Home
[Number => 665-124-2234
)
[2] => Array(
[Player] => Joe
[Type] => Cell
[Number] => 665-321-4325
)
[3] => Array(
[Player] => Joe
[Type] => Home
[Number] => 665-333-1234
)
)
I want Dave's name to display for row [0] because it is the first time it apperars. Then for row [1], I don't want to display his name because it is the same as row [0]. Then for row [2], since row [2]'s name is different than row[1], I want to display Joe's name. Then on row [3], I don't want to dispay Joe's name since it is the same as row [2].

Is there a way to do this in a foreach loop?

Any help on this would be greatly appreciated.
Back to top
View user's profile Send private message
elyod_72
Smarty n00b


Joined: 11 Aug 2008
Posts: 4

PostPosted: Fri Feb 20, 2009 8:18 pm    Post subject: Reply with quote

I figured it out, but it could not be done using foreach. I had to use the section tag instead. Section allows me to use prev_index to compare the previous contents versus the current index contents.

This works great!
Back to top
View user's profile Send private message
draxander
Smarty n00b


Joined: 12 May 2016
Posts: 3

PostPosted: Thu May 12, 2016 10:17 am    Post subject: Reply with quote

Could you explain how you solved this?

I've a similar problem and don't know how to solve it.

(I know, this thread is old Smile )
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu May 12, 2016 10:38 pm    Post subject: Reply with quote

draxander wrote:
(I know, this thread is old Smile )
Necrophilia is bad for your afterlife.
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