dtsnx44260
2017-05-12 03:22
浏览 236
已采纳

使用array_multisort()对数组进行排序[参数#1应该是数组或排序标志]

I have this array of arrays

foreach($qcatResults as $qcatResult){
    ...//codes to get stuff
    $cats[]  = array(
                   'category_code' => $qcatResult->category_code,
                   'category' => $qcatResult->category,
                   'item_count' => $item_count,
                   'max_qty' => (int)$max_qty
               );
}
var_dump($cats);

which would result to something like

array(34) { 
    [0]=> array(4) { ["category_code"]=> string(2) "BB" ["category"]=> string(0) "" 
                     ["item_count"]=> string(1) "1" ["max_qty"]=> int(12000) } 
    [1]=> array(4) { ["category_code"]=> string(2) "AK" ["category"]=> string(6) "Anklet" 
                     ["item_count"]=> string(1) "1" ["max_qty"]=> int(6) } 
    [2]=> array(4) { ["category_code"]=> string(3) "BAC" ["category"]=> string(15) "Bag Accessories" 
                     ["item_count"]=> string(1) "2" ["max_qty"]=> int(352) } 
    [3]=> array(4) { ["category_code"]=> string(2) "WB" ["category"]=> string(4) "Bags" 
                     ["item_count"]=> string(1) "9" ["max_qty"]=> int(6290) } 
    [4]=> array(4) { ["category_code"]=> string(2) "AB" ["category"]=> string(20) "Bathroom Accessories" 
                     ["item_count"]=> string(2) "19" ["max_qty"]=> int(325) } 
    [5]=> array(4) { ["category_code"]=> string(2) "BK" ["category"]=> string(4) "Book" 
                     ["item_count"]=> string(2) "40" ["max_qty"]=>int(27291) }...
}

I want to sort $cats by max_qty in descending order, but array_multisort is giving out the error Message: array_multisort(): Argument #1 is expected to be an array or a sort flag

I have this usage of array_multisort():

var_dump(array_multisort($max_qty, SORT_DESC, $cats));
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtangu8615 2017-05-12 03:33
    已采纳

    I think usort() can do the trick for you:

    $cats=[
        ["category_code"=>"BB","category"=>"","item_count"=>"1","max_qty"=>12000], 
        ["category_code"=>"AK","category"=>"Anklet","item_count"=>"1","max_qty"=>6], 
        ["category_code"=>"BAC","category"=>"Bag Accessories","item_count"=>"2","max_qty"=>352],
        ["category_code"=>"WB","category"=>"Bags","item_count"=>"9","max_qty"=>6290], 
        ["category_code"=>"AB","category"=>"Bathroom Accessories","item_count"=>"19","max_qty"=>325],
        ["category_code"=>"BK","category"=>"Book","item_count"=>"40","max_qty"=>27291]
    ];
    
    usort($cats,function($a,$b){
        return $b['max_qty']-$a['max_qty'];
    });
    
    var_export($cats);
    

    Output:

    array (
      0 => 
      array (
        'category_code' => 'BK',
        'category' => 'Book',
        'item_count' => '40',
        'max_qty' => 27291,
      ),
      1 => 
      array (
        'category_code' => 'BB',
        'category' => '',
        'item_count' => '1',
        'max_qty' => 12000,
      ),
      2 => 
      array (
        'category_code' => 'WB',
        'category' => 'Bags',
        'item_count' => '9',
        'max_qty' => 6290,
      ),
      3 => 
      array (
        'category_code' => 'BAC',
        'category' => 'Bag Accessories',
        'item_count' => '2',
        'max_qty' => 352,
      ),
      4 => 
      array (
        'category_code' => 'AB',
        'category' => 'Bathroom Accessories',
        'item_count' => '19',
        'max_qty' => 325,
      ),
      5 => 
      array (
        'category_code' => 'AK',
        'category' => 'Anklet',
        'item_count' => '1',
        'max_qty' => 6,
      ),
    )
    
    打赏 评论

相关推荐 更多相似问题