PHP Sort a multidimensional array by number of items

后端 未结 3 1978
予麋鹿
予麋鹿 2020-12-09 15:28

I have an array such as:

Array
(
    [DEF] => Array
        (
            [0] => Array
                (
                    [type] => 1
                    


        
3条回答
  •  囚心锁ツ
    2020-12-09 16:17

    Using uksort:

    uksort($array, function($a, $b) { return count($b) - count($a); });
    

    Using array_multisort:

    array_multisort(array_map('count', $array), SORT_DESC, $array);
    

    With PHP < 5.3:

    function sort_cb($a, $b) {
        return count($b) - count($a);
    }
    uksort($array, 'sort_cb');
    

提交回复
热议问题