反向PHP数组

时间:2011-08-17 17:14:41

标签: php mysql

这是代码我正在使用:

<?php

function get_gold_time_noformat()


{
     $goldquery = mysql_query("SELECT * FROM ( 
    SELECT 
        @row := @row +1 AS rownum, time,metal
    FROM ( 
        SELECT @row :=0) r, metal_price 
    WHERE metal = 'GOLD' ORDER BY  `metal_price`.`time` DESC 
) ranked 
WHERE rownum % 16 = 1 
LIMIT 6
");

         while($result = mysql_fetch_array( $goldquery )) 
 { 

    echo "<div id=\"dates\">" . date('g a', strToTime($result['time'] . ' - 7 hours')) .  "</div>";   
 } 

         }
?>

我只是试图反转数组,我尝试使用array_reverse但无法使其工作。

2 个答案:

答案 0 :(得分:2)

不要使用PHP。使用ORDER BY

SELECT * FROM ( 
    SELECT 
        @row := @row +1 AS rownum, time,metal
    FROM ( 
        SELECT @row :=0) r, metal_price 
        WHERE metal = 'GOLD' ORDER BY  `metal_price`.`time` DESC 
    ) ranked 
    WHERE rownum % 16 = 1 
    ORDER BY rownum DESC
    LIMIT 6

答案 1 :(得分:1)

如果在阵列上使用它,则数组反转将起作用。

<?php
function get_gold_time_noformat() {
    $goldquery = mysql_query("SELECT * FROM ( 
        SELECT 
            @row := @row +1 AS rownum, time,metal
        FROM ( 
            SELECT @row :=0) r, metal_price 
            WHERE metal = 'GOLD' ORDER BY  `metal_price`.`time` DESC 
        ) ranked 
        WHERE rownum % 16 = 1 
        LIMIT 6"
    );

    $array_result = array();

    while($result = mysql_fetch_array( $goldquery )) { 
        $array_result[] = $result;
    }

    $array_result = array_reverse( $array_result );

    foreach( $array_result as $result ) {
        echo "<div id=\"dates\">" . date('g a', strToTime($result['time'] . ' - 7 hours')) .  "</div>";   
    } 
}
?>

虽然我建议您查看是否可以在SQL中撤消它。