help to create udt with array_unique function

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
halfdead18
New Member
New Member
Posts: 8
Joined: Sat Oct 25, 2014 11:47 pm

help to create udt with array_unique function

Post by halfdead18 »

Hello,

I have an array $city . it has duplicates inside. I need to create an UDT or some Smarty magic to do array_unique($city); to remove dupicates, and then count the new unique array. Can anybody help with it?
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

Re: help to create udt with array_unique function

Post by Rolf »

- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
Jeff
Power Poster
Power Poster
Posts: 961
Joined: Mon Jan 21, 2008 5:51 pm
Location: MI

Re: help to create udt with array_unique function

Post by Jeff »

try {$city|@array_unique|@count}
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3483
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: help to create udt with array_unique function

Post by velden »

Lot of possibilities in Smarty.

Code: Select all

{* define an array for testing *}
{$array=["b","a","b","c","c"]}

{* print array *}
{$array|print_r}

{* assign modified array to new variable "unique" and print $unique *}
{assign var="unique" value=$array|array_unique}
{$unique|print_r}

{* short way for example above *}
{$unique=$array|array_unique}
{$unique|print_r}

{* you can use php functions too in smarty! *}
{$unique=array_unique($array)}
{$unique|print_r}

{* make unique, count and print*}
{$array|array_unique|count}

{* make unique, count and print - the 'php edition' *}
{count(array_unique($array))}

{* make unique, count and store in "cnt" variable for later/multiple use *}
{$cnt=count(array_unique($array))}
{$cnt}
halfdead18
New Member
New Member
Posts: 8
Joined: Sat Oct 25, 2014 11:47 pm

Re: help to create udt with array_unique function

Post by halfdead18 »

thanks for your reply, works perfectly!

I have another question about combining an array.

I`m getting variables from a {foreach} loop from a list of child pages (using cgsimple get_children).
and they come as a list of arrays. Each loop gives a new array with 1 var.

How can I get this all together in one array? or my array_unique function wount work.
psy
Power Poster
Power Poster
Posts: 463
Joined: Sat Jan 22, 2005 11:19 am

Re: help to create udt with array_unique function

Post by psy »

In your foreach loop, assign the value to a var, eg 'onevar'. Then after each
foreach loop:

Code: Select all

{append var='allvars' value=$onevar}
You can then do your array_unique thing on $allvars.
Post Reply

Return to “The Lounge”