php排序数组的项目数量?

时间:2011-03-31 23:22:33

标签: php arrays sorting multidimensional-array

我有一个澳大利亚州的多维数组,每个州都有项目。我想知道是否可以重新排序数组,以便首先显示具有最多项目的状态?

这是我的阵列,任何帮助将不胜感激:)

[ACT] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 14
                    [name] => Burning Log Megastore
                    [state] => ACT
                    [classid] => 1
                )

            [1] => stdClass Object
                (
                    [id] => 21
                    [name] => Fyshwick Home & Heating
                    [state] => ACT
                    [classid] => 1
                )

        )

    [NSW] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 36
                    [name] => A1 Hot Wood
                    [state] => NSW
                    [classid] => 11
                )

            [1] => stdClass Object
                (
                    [id] => 40
                    [name] => Abbey Fireplaces
                    [state] => NSW
                    [classid] => 1
                )

            [2] => stdClass Object
                (
                    [id] => 55
                    [name] => Almighty Firewood
                    [state] => NSW
                    [classid] => 11
                )

            [3] => stdClass Object
                (
                    [id] => 70
                    [name] => Aussie Tree Services
                    [state] => NSW
                    [classid] => 11
                )

            [4] => stdClass Object
                (
                    [id] => 75
                    [name] => B C Sands
                    [state] => NSW
                    [classid] => 11
                )

            [5] => stdClass Object
                (
                    [id] => 76
                    [name] => B C Sands
                    [state] => NSW
                    [classid] => 11
                )

            [6] => stdClass Object
                (
                    [id] => 77
                    [name] => B C Sands Mascot
                    [state] => NSW
                    [classid] => 11
                )

            [7] => stdClass Object
                (
                    [id] => 113
                    [name] => Barbeques Galore (Maitland)
                    [state] => NSW
                    [classid] => 1
                )

            [8] => stdClass Object
                (
                    [id] => 114
                    [name] => Barbeques Galore Batemans Bay
                    [state] => NSW
                    [classid] => 1
                )

            [9] => stdClass Object
                (
                    [id] => 116
                    [name] => Barbeques Galore Wagga Wagga
                    [state] => NSW
                    [classid] => 1
                )

            [10] => stdClass Object
                (
                    [id] => 131
                    [name] => Best Burning Fire Fuels
                    [state] => NSW
                    [classid] => 11
                )

            [11] => stdClass Object
                (
                    [id] => 132
                    [name] => Betta Burn Firewood
                    [state] => NSW
                    [classid] => 11
                )

            [12] => stdClass Object
                (
                    [id] => 136
                    [name] => Black Forest Firewood
                    [state] => NSW
                    [classid] => 11
                )

            [13] => stdClass Object
                (
                    [id] => 181
                    [name] => Central Coast Brick Supplies Pty Ltd
                    [state] => NSW
                    [classid] => 1
                )

            [14] => stdClass Object
                (
                    [id] => 188
                    [name] => Cheminee Pty Ltd
                    [state] => NSW
                    [classid] => 1
                )

            [15] => stdClass Object
                (
                    [id] => 249
                    [name] => Embers Heating & Restoration Supplies
                    [state] => NSW
                    [classid] => 1
                )

            [16] => stdClass Object
                (
                    [id] => 250
                    [name] => Emmbers Firewood Supplies
                    [state] => NSW
                    [classid] => 11
                )

            [17] => stdClass Object
                (
                    [id] => 292
                    [name] => Goulburn Sand & Soil
                    [state] => NSW
                    [classid] => 11
                )

            [18] => stdClass Object
                (
                    [id] => 304
                    [name] => Gunida Gunyah Aboriginal Corporation
                    [state] => NSW
                    [classid] => 11
                )

            [19] => stdClass Object
                (
                    [id] => 334
                    [name] => J  Perram Firewood Supplies
                    [state] => NSW
                    [classid] => 11
                )

            [20] => stdClass Object
                (
                    [id] => 346
                    [name] => Jetmaster (Aust) Pty Ltd
                    [state] => NSW
                    [classid] => 1
                )

            [21] => stdClass Object
                (
                    [id] => 402
                    [name] => Mark H Newton
                    [state] => NSW
                    [classid] => 4
                )

            [22] => stdClass Object
                (
                    [id] => 416
                    [name] => Might Burn Red Gum Pty Ltd
                    [state] => NSW
                    [classid] => 11
                )

            [23] => stdClass Object
                (
                    [id] => 430
                    [name] => Mr Stoves Pool World
                    [state] => NSW
                    [classid] => 1
                )

            [24] => stdClass Object
                (
                    [id] => 455
                    [name] => O'Brien's Redgum Sawmills
                    [state] => NSW
                    [classid] => 11
                )

            [25] => stdClass Object
                (
                    [id] => 477
                    [name] => PJ & CM Ducat
                    [state] => NSW
                    [classid] => 11
                )

            [26] => stdClass Object
                (
                    [id] => 483
                    [name] => Quality Firewood All Seasons
                    [state] => NSW
                    [classid] => 11
                )

            [27] => stdClass Object
                (
                    [id] => 487
                    [name] => R J & M E Grealy
                    [state] => NSW
                    [classid] => 11
                )

            [28] => stdClass Object
                (
                    [id] => 494
                    [name] => RN & CA Taylor
                    [state] => NSW
                    [classid] => 11
                )

            [29] => stdClass Object
                (
                    [id] => 500
                    [name] => Rouse Hill Firewood
                    [state] => NSW
                    [classid] => 11
                )

            [30] => stdClass Object
                (
                    [id] => 504
                    [name] => Sapphire Coast Firewood
                    [state] => NSW
                    [classid] => 11
                )

            [31] => stdClass Object
                (
                    [id] => 534
                    [name] => Sydney Heaters Pty Ltd
                    [state] => NSW
                    [classid] => 1
                )

            [32] => stdClass Object
                (
                    [id] => 551
                    [name] => The Woodyard
                    [state] => NSW
                    [classid] => 11
                )

            [33] => stdClass Object
                (
                    [id] => 576
                    [name] => W R Campi
                    [state] => NSW
                    [classid] => 11
                )

            [34] => stdClass Object
                (
                    [id] => 602
                    [name] => Wood 4 U
                    [state] => NSW
                    [classid] => 11
                )

            [35] => stdClass Object
                (
                    [id] => 603
                    [name] => Wood Galore
                    [state] => NSW
                    [classid] => 11
                )

            [36] => stdClass Object
                (
                    [id] => 604
                    [name] => Wood Galore
                    [state] => NSW
                    [classid] => 11
                )

        )

    [QLD] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 665
                    [name] => Brightspark Firewood Supplies
                    [state] => QLD
                    [classid] => 11
                )

            [1] => stdClass Object
                (
                    [id] => 702
                    [name] => Gleno's Firewood
                    [state] => QLD
                    [classid] => 11
                )

            [2] => stdClass Object
                (
                    [id] => 704
                    [name] => Gold Coast Fireplace & Barbeque Centre
                    [state] => QLD
                    [classid] => 1
                )

            [3] => stdClass Object
                (
                    [id] => 732
                    [name] => Longburn Wood
                    [state] => QLD
                    [classid] => 11
                )

            [4] => stdClass Object
                (
                    [id] => 742
                    [name] => Out In The Styx
                    [state] => QLD
                    [classid] => 11
                )

            [5] => stdClass Object
                (
                    [id] => 746
                    [name] => Quickfire Firewood
                    [state] => QLD
                    [classid] => 11
                )

        )

    [SA] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 786
                    [name] => Adelaide Wholesale Landscape Supplies
                    [state] => SA
                    [classid] => 11
                )

            [1] => stdClass Object
                (
                    [id] => 825
                    [name] => Clare Wood Yard
                    [state] => SA
                    [classid] => 11
                )

            [2] => stdClass Object
                (
                    [id] => 826
                    [name] => Clewers Electrical and Furniture
                    [state] => SA
                    [classid] => 1
                )

            [3] => stdClass Object
                (
                    [id] => 832
                    [name] => Cullen Transport & Firewood Supplies
                    [state] => SA
                    [classid] => 11
                )

            [4] => stdClass Object
                (
                    [id] => 852
                    [name] => Enviro Systems Renewable Resources Ltd.
                    [state] => SA
                    [classid] => 11
                )

            [5] => stdClass Object
                (
                    [id] => 867
                    [name] => Gawler Landscaping Supplies
                    [state] => SA
                    [classid] => 11
                )

            [6] => stdClass Object
                (
                    [id] => 871
                    [name] => Green Hornet Building & Landscape Supplies
                    [state] => SA
                    [classid] => 11
                )

            [7] => stdClass Object
                (
                    [id] => 899
                    [name] => Mexican Living
                    [state] => SA
                    [classid] => 1
                )

            [8] => stdClass Object
                (
                    [id] => 903
                    [name] => Mount Barker Landscape Centre
                    [state] => SA
                    [classid] => 11
                )

            [9] => stdClass Object
                (
                    [id] => 911
                    [name] => Pecan Engineering Pty Ltd
                    [state] => SA
                    [classid] => 1
                )

            [10] => stdClass Object
                (
                    [id] => 919
                    [name] => PW & JD Plunkett
                    [state] => SA
                    [classid] => 11
                )

            [11] => stdClass Object
                (
                    [id] => 922
                    [name] => Renmark Firewoods & Storage Services
                    [state] => SA
                    [classid] => 11
                )

            [12] => stdClass Object
                (
                    [id] => 930
                    [name] => South Coast Firewood (FWS Inc)
                    [state] => SA
                    [classid] => 11
                )

            [13] => stdClass Object
                (
                    [id] => 937
                    [name] => Tinpak Trading Pty Ltd
                    [state] => SA
                    [classid] => 11
                )

            [14] => stdClass Object
                (
                    [id] => 940
                    [name] => Traeger's Earthmoving & Transport
                    [state] => SA
                    [classid] => 11
                )

            [15] => stdClass Object
                (
                    [id] => 947
                    [name] => Waterways Farm
                    [state] => SA
                    [classid] => 11
                )

        )

    [TAS] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 985
                    [name] => Eastern Tiers Firewood
                    [state] => TAS
                    [classid] => 11
                )

            [1] => stdClass Object
                (
                    [id] => 998
                    [name] => Goods Water & Garden Supplies
                    [state] => TAS
                    [classid] => 11
                )

            [2] => stdClass Object
                (
                    [id] => 1011
                    [name] => K-Mac Wood Supplies
                    [state] => TAS
                    [classid] => 11
                )

            [3] => stdClass Object
                (
                    [id] => 1019
                    [name] => Leslie Vale Landscape & Gravel Supplies
                    [state] => TAS
                    [classid] => 11
                )

            [4] => stdClass Object
                (
                    [id] => 1023
                    [name] => Newmans Heating Shop
                    [state] => TAS
                    [classid] => 1
                )

            [5] => stdClass Object
                (
                    [id] => 1029
                    [name] => Pellet Fires Tasmania
                    [state] => TAS
                    [classid] => 1
                )

        )

    [VIC] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 561
                    [name] => TRT Pastoral Group
                    [state] => VIC
                    [classid] => 11
                )

            [1] => stdClass Object
                (
                    [id] => 1059
                    [name] => A Grade Garden Products
                    [state] => VIC
                    [classid] => 11
                )

            [2] => stdClass Object
                (
                    [id] => 1062
                    [name] => A.F. Gason Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [3] => stdClass Object
                (
                    [id] => 1088
                    [name] => Alpine Timber
                    [state] => VIC
                    [classid] => 11
                )

            [4] => stdClass Object
                (
                    [id] => 1093
                    [name] => Aranbe Heat
                    [state] => VIC
                    [classid] => 1
                )

            [5] => stdClass Object
                (
                    [id] => 1096
                    [name] => ASAP Firewood
                    [state] => VIC
                    [classid] => 11
                )

            [6] => stdClass Object
                (
                    [id] => 1105
                    [name] => Bairnsdale Stoves Heaters & BBQ's
                    [state] => VIC
                    [classid] => 1
                )

            [7] => stdClass Object
                (
                    [id] => 1176
                    [name] => Burra Garden Supplies
                    [state] => VIC
                    [classid] => 11
                )

            [8] => stdClass Object
                (
                    [id] => 1231
                    [name] => Daryl Fagan Firewood
                    [state] => VIC
                    [classid] => 11
                )

            [9] => stdClass Object
                (
                    [id] => 1273
                    [name] => Firefox Industries Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [10] => stdClass Object
                (
                    [id] => 1297
                    [name] => Geoffrey R Gelletly
                    [state] => VIC
                    [classid] => 4
                )

            [11] => stdClass Object
                (
                    [id] => 1301
                    [name] => Gippsland Wood Heater Repairs
                    [state] => VIC
                    [classid] => 1
                )

            [12] => stdClass Object
                (
                    [id] => 1303
                    [name] => Glen Dimplex Australia Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [13] => stdClass Object
                (
                    [id] => 1306
                    [name] => Glowin' Firewood
                    [state] => VIC
                    [classid] => 11
                )

            [14] => stdClass Object
                (
                    [id] => 1328
                    [name] => HRL Technology Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [15] => stdClass Object
                (
                    [id] => 1415
                    [name] => Moran Logging Co Pty Ltd
                    [state] => VIC
                    [classid] => 11
                )

            [16] => stdClass Object
                (
                    [id] => 1427
                    [name] => Murray Industries Kerang
                    [state] => VIC
                    [classid] => 11
                )

            [17] => stdClass Object
                (
                    [id] => 1428
                    [name] => Murray Industries Swan Hill
                    [state] => VIC
                    [classid] => 4
                )

            [18] => stdClass Object
                (
                    [id] => 1436
                    [name] => North East Fwd Strategy Implementn Comm
                    [state] => VIC
                    [classid] => 1
                )

            [19] => stdClass Object
                (
                    [id] => 1438
                    [name] => Northern Landscape Supplies
                    [state] => VIC
                    [classid] => 11
                )

            [20] => stdClass Object
                (
                    [id] => 1448
                    [name] => Park Orchards Garden & Building Supplies
                    [state] => VIC
                    [classid] => 11
                )

            [21] => stdClass Object
                (
                    [id] => 1476
                    [name] => Redgumdave
                    [state] => VIC
                    [classid] => 11
                )

            [22] => stdClass Object
                (
                    [id] => 1493
                    [name] => Sand, Soil & Rocks Pty Ltd
                    [state] => VIC
                    [classid] => 11
                )

            [23] => stdClass Object
                (
                    [id] => 1495
                    [name] => Shamic Sheetmetal Aust  Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [24] => stdClass Object
                (
                    [id] => 1516
                    [name] => Surrey Hills Firewood Supplies
                    [state] => VIC
                    [classid] => 11
                )

            [25] => stdClass Object
                (
                    [id] => 1529
                    [name] => The Flue Factory Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [26] => stdClass Object
                (
                    [id] => 1553
                    [name] => Trevor King
                    [state] => VIC
                    [classid] => 11
                )

            [27] => stdClass Object
                (
                    [id] => 1585
                    [name] => Whitlands Engineering
                    [state] => VIC
                    [classid] => 1
                )

            [28] => stdClass Object
                (
                    [id] => 1591
                    [name] => Wignells of Melbourne Pty Ltd
                    [state] => VIC
                    [classid] => 1
                )

            [29] => stdClass Object
                (
                    [id] => 1604
                    [name] => WWF Australia
                    [state] => VIC
                    [classid] => 1
                )

            [30] => stdClass Object
                (
                    [id] => 1609
                    [name] => Yarra Timber Salvage
                    [state] => VIC
                    [classid] => 11
                )

        )

    [WA] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1623
                    [name] => All Suburbs Garden & Wood Supply
                    [state] => WA
                    [classid] => 11
                )

            [1] => stdClass Object
                (
                    [id] => 1628
                    [name] => Barbecue Bazaar Cannington
                    [state] => WA
                    [classid] => 1
                )

            [2] => stdClass Object
                (
                    [id] => 1672
                    [name] => European Wood Fired Ovens
                    [state] => WA
                    [classid] => 1
                )

            [3] => stdClass Object
                (
                    [id] => 1681
                    [name] => Freo Firewood Supply
                    [state] => WA
                    [classid] => 11
                )

            [4] => stdClass Object
                (
                    [id] => 1740
                    [name] => Roleystone Firewood
                    [state] => WA
                    [classid] => 11
                )

            [5] => stdClass Object
                (
                    [id] => 1747
                    [name] => Smart Burn Pty Ltd
                    [state] => WA
                    [classid] => 1
                )

            [6] => stdClass Object
                (
                    [id] => 1748
                    [name] => Specialised Tree Service
                    [state] => WA
                    [classid] => 11
                )

            [7] => stdClass Object
                (
                    [id] => 1855
                    [name] => York Firewood & Milling
                    [state] => WA
                    [classid] => 11
                )

        )

)

3 个答案:

答案 0 :(得分:3)

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

假设> = PHP 5.3

答案 1 :(得分:1)

运行此命令后,$ arr现在将从最高填充元素排序到更低。

function cmpArray($a, $b)
{
    $countA = count($a);
    $countB = count($b);
    if ($countA == $countB) {
        return 0;
    }
    return ($countA < $countB) ? 1 : -1;
}

uasort($arr, "cmpArray");

答案 2 :(得分:1)

与@ alex的答案类似,但没有匿名函数(所以它适用于PHP&lt; 5.3)

<?php
    $array = array(
        'three' => array(
            'one',
            'two',
            'three'
        ),
        'one' => array(
            'one'
        ),
        'two' => array(
            'one',
            'two'
        ),
    );

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

    uasort($array, 'csort');

    print_r($array);
?>

<强>输出:

Array
(
    [one] => Array
        (
            [0] => one
        )

    [two] => Array
        (
            [0] => one
            [1] => two
        )

    [three] => Array
        (
            [0] => one
            [1] => two
            [2] => three
        )

)