user253938
user253938

Reputation: 172

Sorting array of arrays using array_multisort() [Argument #1 is expected to be an array or a sort flag]

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

Upvotes: 0

Views: 1521

Answers (1)

mickmackusa
mickmackusa

Reputation: 47991

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

Upvotes: 1

Related Questions