duancaozen6066 2015-07-17 03:28
浏览 16
已采纳

如何在PHP中对此数组进行排序?

I've been trying to use the sort() function to rearrange the array from smallest to largest.

This is my print_r of the array which came from serialized data that was imploded:

Array
    (
    [0] => 127173
    [1] => 127172
    [2] => 127174
    [3] => 127175
    [4] => 127178
    [5] => 127176
    [6] => 127177
    [7] => 127179
    [8] => 127180
    [9] => 127183
    [10] => 127181
    )

With sort() and asort() I just get a 1 returning.

  • 写回答

2条回答 默认 最新

  • duanlei7101 2015-07-17 03:37
    关注

    Try this code... in fact the sort function is working fine.

    $array = Array
        (
        '0' => 127173,
        '1' => 127172,
        '2' => 127174,
        '3' => 127175,
        '4' => 127178,
        '5' => 127176,
        '6' => 127177,
        '7' => 127179,
        '8' => 127180,
        '9' => 127183,
        '10' => 127181
        );
    
    sort($array); // <= Sort the array desc
    
    foreach( $array as $key => $value ){
        echo $key."\t=>\t".$value."
    ";
    }
    

    Consider that sort function actually alters your array and returns bool. See doc.

    Check this example online

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?