|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
clj83 Smarty Regular
Joined: 16 Oct 2009 Posts: 44
|
Posted: Wed Apr 02, 2014 8:47 pm Post subject: Sorting Array |
|
|
I am creating an array within a template using:
Code: | {foreach from=$items item=node}
{if $node->fielddefs.location01.value != ''}{$locations[] = $node->fielddefs.location01.value}{/if}
{if $node->fielddefs.location02.value != ''}{$locations[] = $node->fielddefs.location02.value}{/if}
{if $node->fielddefs.location02.value != ''}{$locations[] = $node->fielddefs.location03.value}{/if}
{/foreach} |
I need to sort the array alphabetically. I have tried the following usort function:
Code: | if (!function_exists('do_sort')) {
function do_sort($a, $b) {
return $a > $b;
}
}
$data = $params['data'];
usort($data, 'do_sort');
$smarty->assign('sorted', $data); |
It does now work because my array is not correct I guess? My array looks like this:
Code: | Array
(
[0] => Any
[1] => Chamonix
[2] => Courmayeur
[3] => Verbier
[4] => Zermatt
[5] => Verbier
[6] => Chamonix
) |
Can anyone help me achieve a sorted output?
Thanks |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
|
Back to top |
|
clj83 Smarty Regular
Joined: 16 Oct 2009 Posts: 44
|
Posted: Wed Apr 02, 2014 9:25 pm Post subject: |
|
|
Thanks for your help. I would use sort if I could figure it out, I am a php newbie. I don't suppose you could help me create the sort function?
Thanks |
|
Back to top |
|
clj83 Smarty Regular
Joined: 16 Oct 2009 Posts: 44
|
Posted: Wed Apr 02, 2014 9:27 pm Post subject: |
|
|
Actually, think I have it. Does this look right?
Code: | $data = $params['data'];
sort($data);
foreach ($data as $key => $val) {
}
$smarty->assign('sorted', $data); |
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Apr 02, 2014 9:40 pm Post subject: |
|
|
you don't need
Code: | foreach ($data as $key => $val) {
} | as it does nothing. |
|
Back to top |
|
|
|
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
|