Tutorial :Is there a specific smarty function to order alphabetically an array?


I'm using a smarty template for a multi-language website. I got an array of country that is order by country code, which is ok for the english version as country name are in the right order but no ok for other languages (for example United Kingdom stay in the "U" whereas in French it prints "Royaume Uni".

Is there a smarty function to order an array alphabetically?


You need to sort the before assigning it in smarty as such:

asort($countryList);  $smarty->assign($countryList);  


  • asort() to sort the array by value.
  • ksort() to sort the array by keys.


You can apply a modifier to an array in Smarty like this (the @ prefix means the modifier is applied to the whole array, not each element):


asort() won't work as a modifier however, because it modifies the passed array and returns a boolean rather than returning a modified array. You could however define your own function and use that as a modifier, e.g.

function sort_array($array) {      asort($array);      return $array;     }  

Then in Smarty you can do something like

{foreach from=$array|@sort_array item=val}      {$val}  {/foreach}  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »