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

in_array in smarty

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


Joined: 12 Oct 2003
Posts: 1

PostPosted: Sun Oct 12, 2003 4:23 am    Post subject: in_array in smarty Reply with quote

Hi guys, just wondering if we can use the in_array function in smarty to display arrays. One array will display available categories and the other array will display selected categories. The in_array will display the available categories that have not been assigned.

Thankyou for your help in advance

Danny
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sun Oct 12, 2003 4:47 am    Post subject: Reply with quote

Perhaps consider building a pre-computed array of unselected items with array_diff?

If you are looking to use in_array in a {if}, then it is easy since PHP functions are allowed as modifiers when security is not enabled (which is the default):
Code:
{if $current_item|in_array($selected_array)}
Back to top
View user's profile Send private message
lukas
Smarty Rookie


Joined: 23 Sep 2006
Posts: 5
Location: Germany

PostPosted: Sat Sep 23, 2006 7:04 pm    Post subject: Reply with quote

Hi,

I just came across the exact same problem and found this thread on google, so sorry for a reply three years after the thread was started ^^.

I have the following code inside a for-each loop:
[php]{foreach from=$agegroup item=agegroup name=agegroup}
<td>
<label for="agegroup-{$smarty.foreach.agegroup.iteration}">
<input type="checkbox" name="altersklasse-{$smarty.foreach.agegroup.iteration}" value="{$agegroup.id}" id="agegroup-{$smarty.foreach.agegroup.iteration}" {if $agegroup.id|in_array($agegroupSelected)}checked{/if} /> //This is line 70
{$agegroup.kuerzel}</label>
</td>
{if $smarty.foreach.agegroup.iteration == 6} </tr><tr>{/if}
{/foreach}
[/php]

When I run this code, i get the following error message:
Quote:
Parse error: syntax error, unexpected '(' in /<ADRESS-TO-FILE>/templates_c/module^veranstaltungen^veranstaltung^%%7B^7B3^7B308EF8%%hinzufuegen.tpl.php on line 70


I am fairly new to smarty, what could be the reason for this error?
If i'm not mistaken, i should be allowed to put bracktes inside the {if}-Statement...or?

Would be awesome if you could help me solve this problem.

Lukas


Last edited by lukas on Tue Jan 02, 2007 7:44 pm; edited 1 time in total
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sun Sep 24, 2006 6:10 am    Post subject: Reply with quote

My bad.

The correct modifier syntax is:
Code:
{if $current_item|in_array:$selected_array}


or just use the php function as usual:
Code:
{if in_array($current_item,$selected_array}


I hate being shown up 3 years after the fact, but that's the beauty of the internet, huh? Wink

HTH

PS: do read the relevant portions of the manual on modifiers and if statements for the details. In fact, a quick read of the manual will give you lots of good stuff to build from.
Back to top
View user's profile Send private message
deadcat
Smarty Rookie


Joined: 20 Sep 2003
Posts: 10

PostPosted: Thu Dec 13, 2007 1:53 am    Post subject: Reply with quote

I also dont want to reply after more than one year later, but really want to say thanks!
It helps me alot Very Happy
Back to top
View user's profile Send private message
vietnux
Smarty n00b


Joined: 02 Apr 2011
Posts: 2

PostPosted: Tue May 31, 2011 4:35 am    Post subject: Reply with quote

i don't using all,
i using 'foreach' in array to search in array
Back to top
View user's profile Send private message Visit poster's website
vietnux
Smarty n00b


Joined: 02 Apr 2011
Posts: 2

PostPosted: Tue May 31, 2011 4:42 am    Post subject: Reply with quote

vietnux wrote:
i don't using all,
i using 'foreach' in array to search in array


sorry all!
i used to be way is "in_array($var, $aryEvent.partner)"
Back to top
View user's profile Send private message Visit poster's website
dgloria
Smarty n00b


Joined: 10 Mar 2022
Posts: 1

PostPosted: Thu Mar 10, 2022 4:18 pm    Post subject: in_array Reply with quote

Code:
{if in_array($needle, $haystack)} found $needle {/if}
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