Page 1 of 1

help to create udt with array_unique function

Posted: Mon Jan 12, 2015 11:18 pm
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?

Re: help to create udt with array_unique function

Posted: Tue Jan 13, 2015 6:04 am
by Rolf

Re: help to create udt with array_unique function

Posted: Tue Jan 13, 2015 9:08 pm
by Jeff
try {$city|@array_unique|@count}

Re: help to create udt with array_unique function

Posted: Tue Jan 13, 2015 10:00 pm
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}

Re: help to create udt with array_unique function

Posted: Wed Jan 14, 2015 4:03 pm
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.

Re: help to create udt with array_unique function

Posted: Thu Jan 15, 2015 10:57 pm
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.