2

我有这个数组:

Array
(
  [0] => Array
  (
    [company_id] => 1
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [1] => Array
  (
    [company_id] => 3
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [2] => Array
  (
    [company_id] => 4
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [3] => Array
  (
    [company_id] => 5
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [4] => Array
  (
    [company_id] => 6
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [5] => Array
  (
    [company_id] => 7
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [6] => Array
  (
    [company_id] => 11
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [7] => Array
  (
    [company_id] => 12
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [8] => Array
  (
    [company_id] => 13
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [9] => Array
  (
    [company_id] => 14
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [10] => Array
  (
    [company_id] => 15
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [11] => Array
  (
    [company_id] => 16
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [12] => Array
  (
    [company_id] => 17
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [13] => Array
  (
    [company_id] => 18
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [14] => Array
  (
    [company_id] => 19
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [15] => Array
  (
    [company_id] => 20
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [16] => Array
  (
    [company_id] => 21
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [17] => Array
  (
    [company_id] => 22
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [18] => Array
  (
    [company_id] => 23
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [19] => Array
  (
    [company_id] => 25
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [20] => Array
  (
    [company_id] => 26
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [21] => Array
  (
  [company_id] => 27
  [avg_company] =>
  [avg_product] =>
  [avg_service] =>
  [avg_price] =>
  [avg_rating] => 0
  )

  [22] => Array
  (
    [company_id] => 28
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [23] => Array
  (
    [company_id] => 29
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [24] => Array
  (
    [company_id] => 30
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [25] => Array
  (
    [company_id] => 31
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [26] => Array
  (
    [company_id] => 32
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [27] => Array
  (
    [company_id] => 33
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [28] => Array
  (
    [company_id] => 34
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [29] => Array
  (
    [company_id] => 35
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [30] => Array
  (
    [company_id] => 36
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [31] => Array
  (
    [company_id] => 37
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [32] => Array
  (
    [company_id] => 38
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [33] => Array
  (
    [company_id] => 39
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [34] => Array
  (
    [company_id] => 40
    [avg_company] => 4.00000
    [avg_product] => 2.00000
    [avg_service] => 5.00000
    [avg_price] => 5.00000
    [avg_rating_remark] => Solid
    [avg_rating] => 4.0
  )

  [35] => Array
  (
    [company_id] => 41
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [36] => Array
  (
    [company_id] => 42
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [37] => Array
  (
    [company_id] => 43
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [38] => Array
  (
    [company_id] => 44
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [39] => Array
  (
    [company_id] => 45
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [40] => Array
  (
    [company_id] => 46
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [41] => Array
  (
    [company_id] => 47
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [42] => Array
  (
    [company_id] => 48
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [43] => Array
  (
    [company_id] => 49
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [44] => Array
  (
    [company_id] => 50
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [45] => Array
  (
    [company_id] => 51
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [46] => Array
  (
    [company_id] => 52
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [47] => Array
  (
    [company_id] => 53
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [48] => Array
  (
    [company_id] => 55
    [avg_company] => 4.50000
    [avg_product] => 4.50000
    [avg_service] => 4.50000
    [avg_price] => 3.00000
    [avg_rating_remark] => Solid
    [avg_rating] => 4.1
  )

  [49] => Array
  (
    [company_id] => 58
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [50] => Array
  (
    [company_id] => 61
    [avg_company] => 5.00000
    [avg_product] => 5.00000
    [avg_service] => 5.00000
    [avg_price] => 5.00000
    [avg_rating_remark] => Excellent
    [avg_rating] => 5.0
  )

  [51] => Array
  (
    [company_id] => 62
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )
)

我想对键值avg_rating进行降序排序,所以我根据其他 SO 问题使用它:

usort($ret, function($a, $b) {
  return $a['avg_rating'] - $b['avg_rating'];
});

但是结果不对:

Array
(
  [0] => Array
  (
    [company_id] => 39
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [1] => Array
  (
    [company_id] => 38
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [2] => Array
  (
    [company_id] => 41
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [3] => Array
  (
    [company_id] => 42
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [4] => Array
  (
    [company_id] => 43
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [5] => Array
  (
    [company_id] => 37
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [6] => Array
  (
    [company_id] => 36
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [7] => Array
  (
    [company_id] => 32
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [8] => Array
  (
    [company_id] => 33
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [9] => Array
  (
    [company_id] => 34
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [10] => Array
  (
    [company_id] => 35
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [11] => Array
  (
    [company_id] => 44
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [12] => Array
  (
    [company_id] => 45
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [13] => Array
  (
    [company_id] => 52
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [14] => Array
  (
    [company_id] => 53
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [15] => Array
  (
    [company_id] => 58
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [16] => Array
  (
    [company_id] => 62
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [17] => Array
  (
    [company_id] => 51
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [18] => Array
  (
    [company_id] => 50
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [19] => Array
  (
    [company_id] => 46
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [20] => Array
  (
    [company_id] => 47
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [21] => Array
  (
    [company_id] => 48
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [22] => Array
  (
    [company_id] => 49
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [23] => Array
  (
    [company_id] => 1
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [24] => Array
  (
    [company_id] => 31
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [25] => Array
  (
    [company_id] => 13
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [26] => Array
  (
    [company_id] => 12
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [27] => Array
  (
    [company_id] => 14
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [28] => Array
  (
    [company_id] => 15
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [29] => Array
  (
    [company_id] => 16
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [30] => Array
  (
    [company_id] => 11
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [31] => Array
  (
    [company_id] => 7
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [32] => Array
  (
    [company_id] => 3
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [33] => Array
  (
    [company_id] => 4
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [34] => Array
  (
    [company_id] => 5
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [35] => Array
  (
    [company_id] => 6
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [36] => Array
  (
    [company_id] => 30
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [37] => Array
  (
    [company_id] => 17
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [38] => Array
  (
    [company_id] => 26
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [39] => Array
  (
    [company_id] => 25
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [40] => Array
  (
    [company_id] => 27
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [41] => Array
  (
    [company_id] => 28
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [42] => Array
  (
    [company_id] => 29
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [43] => Array
  (
    [company_id] => 18
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [44] => Array
  (
    [company_id] => 23
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [45] => Array
  (
    [company_id] => 20
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [46] => Array
  (
    [company_id] => 22
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [47] => Array
  (
    [company_id] => 19
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [48] => Array
  (
    [company_id] => 21
    [avg_company] =>
    [avg_product] =>
    [avg_service] =>
    [avg_price] =>
    [avg_rating] => 0
  )

  [49] => Array
  (
    [company_id] => 61
    [avg_company] => 5.00000
    [avg_product] => 5.00000
    [avg_service] => 5.00000
    [avg_price] => 5.00000
    [avg_rating_remark] => Excellent
    [avg_rating] => 5.0
  )

  [50] => Array
  (
    [company_id] => 55
    [avg_company] => 4.50000
    [avg_product] => 4.50000
    [avg_service] => 4.50000
    [avg_price] => 3.00000
    [avg_rating_remark] => Solid
    [avg_rating] => 4.1
  )

  [51] => Array
  (
    [company_id] => 40
    [avg_company] => 4.00000
    [avg_product] => 2.00000
    [avg_service] => 5.00000
    [avg_price] => 5.00000
    [avg_rating_remark] => Solid
    [avg_rating] => 4.0
  )
)
4

3 回答 3

6

我在这个问题上应用了解决方案,将我的usort()语法替换为:

usort($ret, function($a, $b) {
  $a = $a['avg_rating'];
  $b = $b['avg_rating'];
if ($a == $b) { return 0; }
  return ($a < $b) ? -1 : 1;
});

然后我做array_reverse()一个$ret

$ret = array_reverse($ret);
于 2013-01-02T07:46:48.373 回答
5

用户回调函数期望返回整数,但这个有时会尝试返回浮点数,它会自动四舍五入。尝试

usort($ret, function($a, $b) {
  return $b['avg_rating'] > $a['avg_rating'];
});
于 2013-01-02T07:18:24.227 回答
5

我所做的是我知道我的浮点数总是有一个小数点,所以这就是我所做的:

usort($dataArray,function($a,$b){

    return ($a*10) - ($b*10);
}); 
于 2014-07-15T00:57:36.620 回答