最后将数组排序到第一个

时间:2014-07-23 08:52:03

标签: php arrays sorting

我有一个数组现在我想按升序对数组进行排序。我尝试但它确实有效。我的数组是

   Array
    (
        [0] => Array
            (
                [0] => 216
                [id] => 216
                [1] => 52.42
                [TRUNCATE(100*(sum(m.carbs/(m.carbs+m.fat+m.protein))/count(u.id)),2)] => 52.42
                [2] => 2014-07-22
                [Cdate] => 2014-07-22
            )

        [1] => Array
            (
                [0] => 217
                [id] => 217
                [1] => 73.91
                [TRUNCATE(100*(sum(m.carbs/(m.carbs+m.fat+m.protein))/count(u.id)),2)] => 73.91
                [2] => 2014-07-19
                [Cdate] => 2014-07-19 
            )

        [2] => Array
            (
                [0] => 220
                [id] => 220
                [1] => 71.87
                [TRUNCATE(100*(sum(m.carbs/(m.carbs+m.fat+m.protein))/count(u.id)),2)] => 71.87
                [2] => 2014-07-18
                [Cdate] => 2014-07-18
            )

现在我想将我的数组排序为最后一个索引

2,1,0

我已经写了功能但没有工作

function lastSort($a, $b) {
    $aLast = end(explode(' ', $a));
    $bLast = end(explode(' ', $b));

    return strcasecmp($aLast, $bLast);
}

$r=uasort($array, 'lastSort');diamen

2 个答案:

答案 0 :(得分:0)

使用array_reverse http://php.net/manual/en/function.array-reverse.php

<?php

$array = array(0 => array("foo" => "bar"),
               1 => array("faz" => "baz"),
               2 => array("x" => "y")
              );

print_r( array_reverse($array, true) );

结果:

Array
(
    [2] => Array
        (
            [x] => y
        )

    [1] => Array
        (
            [faz] => baz
        )

    [0] => Array
        (
            [foo] => bar
        )

)

答案 1 :(得分:0)

因为您想要的是反转数组,为什么不使用PHP本机函数array_reverse?

$array = array(/*... Your array*/);
$r = array_reverse($array);

这可以节省你: array_reverse()