Search code examples
phpsmarty

Arrange list in reverse_array within {foreach}


I am using {foreach} within smarty like this

{foreach key=num item=reply from=$replies}
//something goes here.
{/foreach}

Currently I am getting replies arranged like...

Older --> Old --> New --> Newer

I want to arrange them in this order

Newer --> New --> Old --> Older

How to achieve this ?

Thanks

Solved

Thanks to ts for this

from=$replies|@array_reverse

& Required following smarty plugin

modifier.reverse_array.php

<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty reverse_array modifier plugin
 *
 * Type:     modifier<br>
 * Name:     reverse_array<br>
 * Purpose:  reverse arrays
 * @author   Noel McGran 
 * @param array
 * @return array
 */
function smarty_modifier_reverse_array($array)
{
    return array_reverse($array);
}

/* vim: set expandtab: */

?>

Solution

  • This will solve the problem:

    from=$replies|@array_reverse