|
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 |
sarahfoxnz Smarty Regular
Joined: 11 Apr 2010 Posts: 76
|
Posted: Fri Dec 27, 2019 3:56 am Post subject: Built in sort function ? |
|
|
smarty-3.1.33
Hello. I am wondering if there is a later version of Smarty, and if so - Does it contain a BUILT IN sort function for arrays? (the last i saw there was no built-in function).
Ive got an array with
- TOP LEVEL,
-- SECOND LEVEL.
each element of TOP LEVEL contains 1 or more SECOND LEVEL items, which i want to sort within the template.
Is this possible in the latest version ? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
|
Back to top |
|
sarahfoxnz Smarty Regular
Joined: 11 Apr 2010 Posts: 76
|
Posted: Sat Dec 28, 2019 12:03 am Post subject: |
|
|
AnrDaemon wrote: | https://www.smarty.net/forums/viewtopic.php?p=94753#94753 |
Whats this got to do with sorting an array ? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sat Dec 28, 2019 1:33 am Post subject: |
|
|
Scanning all the Smarty code for 'sort' produced two results that pertain to sorting the names of the assigned variables that will eventually display in the debug popup window.
So, I think there is no Smarty-developed sorting function.
If your array is something like:
$myArray: array(
array('a','b','c'),
array('b','c','d'),
...
)
That is, an array(s) of arrays, then maybe iterate through the first dimension, using a PHP function to sort each second dimension.
But I cannot find any BUILT-IN sorting function within the core Smarty package. |
|
Back to top |
|
sarahfoxnz Smarty Regular
Joined: 11 Apr 2010 Posts: 76
|
Posted: Sat Dec 28, 2019 2:18 am Post subject: |
|
|
Found it - Include this before you do FOREACH (in template)
{$tmp = ksort($list)}
$list is the array you are foreaching (is that a word)
Code: |
{$tmp = ksort($list)}
{foreach from=$list key=k item=v}
.. do stuff..
{/foreach}
|
EDIT - i dont use $tmp in any way - except to sort $list (or whatever variable) |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Dec 28, 2019 12:41 pm Post subject: |
|
|
sarahfoxnz wrote: | AnrDaemon wrote: | https://www.smarty.net/forums/viewtopic.php?p=94753#94753 |
Whats this got to do with sorting an array ? |
http://xyproblem.info/
If you really want to sort data, do it in your code, Smarty shouldn't care about such things.
If you want to show menu in a specific order, use specific code that displays your menu. |
|
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
|