Arrange list in reverse_array within {foreach}

12,562

Solution 1

This will solve the problem:

from=$replies|@array_reverse

Solution 2

Check out array_reverse() ;)

if not, you could simply put data on a new array (or whatever structure you are using) with foreach and array_pop() then you have it in the other way ;) stack vs queue

Share:
12,562
MANnDAaR
Author by

MANnDAaR

Self taught web developer

Updated on June 08, 2022

Comments

  • MANnDAaR
    MANnDAaR almost 2 years

    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: */
    
    ?>