Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrange list in reverse_array within {foreach}

Tags:

php

smarty

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

?>
like image 991
MANnDAaR Avatar asked Aug 01 '10 15:08

MANnDAaR


Video Answer


2 Answers

This will solve the problem:

from=$replies|@array_reverse
like image 195
ts. Avatar answered Oct 20 '22 02:10

ts.


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

like image 26
Saikios Avatar answered Oct 20 '22 01:10

Saikios