Sort array modifier

  Previous article Next article  

Sort array modifier

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.2
  Last updated: 05-02-2017
  Comments: 2

Sorting the output of an array with this simple Smarty modifier. Sometimes has a problem, a very simple solution...
I was grateful inspired by http://stackoverflow.com when making this.

Create a file named "modifier.sort_array.php" and add it to the CMS Made Simple /plugins folder

<?php
/**
 * Smarty sort_array modifier plugin
 *
 * Type:     modifier
 * Name:     sort_array
 * Purpose:  alphabetize an array
 *
 * https://www.cmscanbesimple.org/blog/sort-array-modifier
 */


function smarty_modifier_sort_array($array)
{
    asort($array);
    return $array;  
}
?>

  How to use

You can alphabetize the output in your template like:

{foreach from=$item|sort_array item='bar'}
  {$bar}
{/foreach}

  Working example


A2 Webhosting CMS Made Simple

  Comments

  Click here to leave a comment






This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
  « Enter the CAPTCHA code (lower case)

Your IP address will be stored on submit. I reserve the right to change or delete your response without notice!
In case of abuse your IP address will be blocked!


Can I have your attention for a moment?

Did this tutorial help you solving a problem at your (clients) website and it saved you many hours of work? Great!!
Consider buying Rolf a cup of coffee in return! [ Click here ]


  2 Comments

CMS Made Simple 2.2.2 - Tutorials, tips and tricks - CMSMS 2.2.2

Sort array modifier

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.2
  Last updated: 05-02-2017
  Comments: 2

A2 Webhosting CMS Made Simple