How can a two-dimensional array find the one with the largest number of elements in the array and put it at the front of the array?

array:2 [
  "aa" => array:6 [
    "comboNameId" => 1
    "sku" => "aa"
    "lineItem" => "x"
    "qty" => 1
    "lineItem1" => "y"
    "qty1" => 2
  ]
  "bb" => array:8 [
    "comboNameId" => 1
    "sku" => "bb"
    "lineItem" => "111"
    "qty" => 111
    "lineItem1" => "222"
    "qty1" => 2
    "lineItem2" => "333"
    "qty2" => 3
  ]
]

this is an array I got in a loop. Now I need to find the one with the largest number of this two-dimensional array and put it at the top of the array. I don"t need to sort the array. May I ask how to achieve it?

Php
Mar.29,2021

`
/ / $arr is the original array, and temp is the temporary saved array
foreach ($arr as $k = > $v) {

$temp[$k] = count($v);

}
/ / the order of the number of arrays in front of multiple rows
arsort ($temp);
/ / reassemble $res, is the target result
foreach ($temp as $k = > $v) {

$res[$k] = $arr[$k];

}
`

clipboard.png


uasort($arr,function($a,$b){ 
    $a = count($a); 
    $b = count($b);
    return ($a<$b)?1:(($a==$b)?0:-1);
});
Menu