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
  )
)

Key-Value 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

ユーザーコールバック関数は整数を返すことが期待されていますが、これは自動的に丸められるfloatを返そうとすることがあります。試す

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

私がしたことは、フロートには常に小数点が1つあることを知っていたので、これが私がしたことです。

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

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