代码之家  ›  专栏  ›  技术社区  ›  Nidhoegger

在php 7中对关联数组排序[重复]

  •  0
  • Nidhoegger  · 技术社区  · 6 年前

    这个问题已经有了答案:

    给出以下示例:

    $user1 = array('username' => 'test1', 'score' => 2000, 'someotherdata' => 1.0);
    $user2 = array('username' => 'test2', 'score' => 4325, 'someotherdata' => 2.0);
    $user3 = array('username' => 'test3', 'score' => 624534, 'someotherdata' => 3.0);
    $user4 = array('username' => 'test4', 'score' => 564, 'someotherdata' => 1.4);
    $user5 = array('username' => 'test5', 'score' => 34256, 'someotherdata' => 1.5);
    $user6 = array('username' => 'test6', 'score' => 5476, 'someotherdata' => 1.8);
    
    $arr = array($user1, $user2, $user3, $user4, $user5, $user6);
    

    我怎么能分类 $arr 在田野边 score 在php 7中表现不错?我有一个自己制作的bubblesort,但是有没有一种方法可以使用内置的php 7功能来很好地实现它,因为bubblesort非常昂贵(我可以自己做一个quicksort,但是在我这样做之前,我想知道是否有更好的方法)。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Eddie    6 年前

    你可以用 usort

    $user1 = array('username' => 'test1', 'score' => 2000, 'someotherdata' => 1.0);
    $user2 = array('username' => 'test2', 'score' => 4325, 'someotherdata' => 2.0);
    $user3 = array('username' => 'test3', 'score' => 624534, 'someotherdata' => 3.0);
    $user4 = array('username' => 'test4', 'score' => 564, 'someotherdata' => 1.4);
    $user5 = array('username' => 'test5', 'score' => 34256, 'someotherdata' => 1.5);
    $user6 = array('username' => 'test6', 'score' => 5476, 'someotherdata' => 1.8);
    
    $arr = array($user1, $user2, $user3, $user4, $user5, $user6);
    
    usort($arr, function($a, $b){
        return $a['score'] - $b['score'];
    });
    
    echo "<pre>";
    print_r( $arr );
    echo "</pre>";
    

    这将导致:

    Array
    (
        [0] => Array
            (
                [username] => test4
                [score] => 564
                [someotherdata] => 1.4
            )
    
        [1] => Array
            (
                [username] => test1
                [score] => 2000
                [someotherdata] => 1
            )
    
        [2] => Array
            (
                [username] => test2
                [score] => 4325
                [someotherdata] => 2
            )
    
        [3] => Array
            (
                [username] => test6
                [score] => 5476
                [someotherdata] => 1.8
            )
    
        [4] => Array
            (
                [username] => test5
                [score] => 34256
                [someotherdata] => 1.5
            )
    
        [5] => Array
            (
                [username] => test3
                [score] => 624534
                [someotherdata] => 3
            )
    
    )
    

    Doc: usort()

        2
  •  1
  •   Bhumi Shah    6 年前

    使用多重排序

    $user1 = array('username' => 'test1', 'score' => 2000, 'someotherdata' => 1.0);
    $user2 = array('username' => 'test2', 'score' => 4325, 'someotherdata' => 2.0);
    $user3 = array('username' => 'test3', 'score' => 624534, 'someotherdata' => 3.0);
    $user4 = array('username' => 'test4', 'score' => 564, 'someotherdata' => 1.4);
    $user5 = array('username' => 'test5', 'score' => 34256, 'someotherdata' => 1.5);
    $user6 = array('username' => 'test6', 'score' => 5476, 'someotherdata' => 1.8);
    
    $arr = array($user1, $user2, $user3, $user4, $user5, $user6);
    
    $score = array();
    foreach ($arr as $key => $row)
    {
        $score[$key] = $row['score'];
    }
    array_multisort($score, SORT_DESC, $arr);
    echo "<pre>";
    print_r($score);
    echo "</pre>";