排序数组并根据某些值排列

时间:2015-12-12 08:40:55

标签: php arrays

我有一个像这样的数组

 [Defense] => Array
    (
        [0] => stdClass Object
            (
                [id] => 1
                [position] => G 
                [point] => 1.6

            )
        [1] => stdClass Object
            (
                [id] => 1
                [position] => F 
                [point] => 2.7
            )


        [2] => stdClass Object
            (
                [id] => 1
                [position] => C 
                [point] => 1.4
            )
        [3] => stdClass Object
            (
                [id] => 2
                [position] => C 
                [point] => 1.7
            )

        [4] => stdClass Object
            (
                [id] => 2
                [position] => D 
                [point] => 1.1
            )
        [5] => stdClass Object
            (
                [id] => 1
                [position] => F 
                [point] => 1.7
            ) 

        [6] => stdClass Object
            (
                [id] => 1
                [position] => G 
                [point] => 1.6

            )

        [7] => stdClass Object
            (
                [id] => 1
                [position] => G 
                [point] => 1.8

            )
       [8] => stdClass Object
        (
                [id] => 2
                [position] => D
                [point] => 1.5
        )   
  )

我想根据descending中的PHP点对数组进行排序。此外,所有位置都按顺序排列(所有G都比F所有......所以......) 像这样:

  [Defense] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [position] => G 
                    [point] => 1.8

                )
             [1] => stdClass Object
                (
                    [id] => 1
                    [position] => G 
                    [point] => 1.6

                )
            [2] => stdClass Object
                (
                    [id] => 1
                    [position] => G 
                    [point] => 1.6

                )   
            [3] => stdClass Object
                (
                    [id] => 1
                    [position] => F 
                    [point] => 2.7
                )
            [4] => stdClass Object
                (
                    [id] => 1
                    [position] => F 
                    [point] => 1.7
                )

            [5] => stdClass Object
                (
                    [id] => 1
                    [position] => C 
                    [point] => 1.7
                )
            [6] => stdClass Object
                (
                    [id] => 2
                    [position] => C 
                    [point] => 1.4 
                )
            [7] => stdClass Object
                (
                    [id] => 2
                    [position] => D
                    [point] => 1.5
                )
            [8] => stdClass Object
                (
                    [id] => 2
                    [position] => D 
                    [point] => 1.1
                )

      )

任何帮助?

1 个答案:

答案 0 :(得分:1)

使用usort()函数对数组进行排序,如下所示

usort($array,function($a,$b){
if($a->position == $b->position )
    return $a->point < $b->point;
else
    return $a->position < $b->position;
});
var_dump($array);