根据键[重复]对多维关联数组进行排序

This question already has an answer here:

Hi I have an array like the following:

$originalArray = [
    'Furniture/Fixture' => [
        [
            'LedgerId' => '557af7adc3acc3ac2b8b4567',
            'groupName' => 'Furniture/Fixture',
            'weightage' => 29,
            'ledgerName' => 'Furniture & Fixture',
        ],
        [
            'LedgerId' => '558ffa02c3acc331258b4567',
            'groupName' => 'Furniture/Fixture',
            'weightage' => 28,
            'ledgerName' => 'Water Coolers,Referigerator',
        ],
    ],
    'Building' => [
        [
            'LedgerId' => '5586c5eec3acc3f42c8b4567',
            'groupName' => 'Building',
            'weightage' => 26,
            'ledgerName' => 'Main Building',
        ],
        [
            'LedgerId' => '5586c85cc3acc38e2e8b4567',
            'groupName' => 'Building',
            'weightage' => 22,
            'ledgerName' => 'Jr.School Building',
        ],
    ],
    'Vehicle' => [
        [
            'LedgerId' => '55898004c3acc38b758b4567',
            'groupName' => 'Vehicle',
            'weightage' => 37,
            'ledgerName' => 'School Bus',
        ],
        [
            'LedgerId' => '55898078c3acc3c5758b4567',
            'groupName' => 'Vehicle',
            'weightage' => 27,
            'ledgerName' => 'Staff Car - TATA Mobile',
        ],
    ],
    'Plant & Machinery' => [
        [
            'LedgerId' => '557af81fc3acc3ed2b8b4567',
            'groupName' => 'Plant & Machinery',
            'weightage' => 18,
            'ledgerName' => 'Tools For Carpentres',
        ],
        [
            'LedgerId' => '557afff7c3acc38d2e8b4567',
            'groupName' => 'Plant & Machinery',
            'weightage' => 29,
            'ledgerName' => 'Water Heating Equipment',
        ],
        [
            'LedgerId' => '557b004dc3acc3bf2e8b4567',
            'groupName' => 'Plant & Machinery',
            'weightage' => 28,
            'ledgerName' => 'Mess Cold Room',
        ],
    ],
];

I want to sort each associative array, like Furniture/Fixture and Buildings, using the key inside each array weightage. How can I solve it, so that the output looks like this?

Array
([Building] => Array
    (
        [0] => Array
            (
                [LedgerId] => 5586c5eec3acc3f42c8b4567
                [groupName] => Building
                [weightage] => 26
                [ledgerName] => Main Building
            )

        [1] => Array
            (
                [LedgerId] => 5586c85cc3acc38e2e8b4567
                [groupName] => Building
                [weightage] => 26
                [ledgerName] => Jr.School Building
            )
    )
[Vehicle] => Array
    (
        [0] => Array
            (
                [LedgerId] => 55898004c3acc38b758b4567
                [groupName] => Vehicle
                [weightage] => 27
                [ledgerName] => School Bus
            )

        [1] => Array
            (
                [LedgerId] => 55898078c3acc3c5758b4567
                [groupName] => Vehicle
                [weightage] => 27
                [ledgerName] => Staff Car - TATA Mobile
            )            

    )
[Plant & Machinery] => Array
    (
        [0] => Array
            (
                [LedgerId] => 557af81fc3acc3ed2b8b4567
                [groupName] => Plant & Machinery
                [weightage] => 28
                [ledgerName] => Tools For Carpentres
            )

        [1] => Array
            (
                [LedgerId] => 557afff7c3acc38d2e8b4567
                [groupName] => Plant & Machinery
                [weightage] => 28
                [ledgerName] => Water Heating Equipment
            )


    )
[Furniture/Fixture] => Array
    (
        [0] => Array
            (
                [LedgerId] => 557af7adc3acc3ac2b8b4567
                [groupName] => Furniture/Fixture
                [weightage] => 29
                [ledgerName] => Furniture & Fixture
            )

        [1] => Array
            (
                [LedgerId] => 558ffa02c3acc331258b4567
                [groupName] => Furniture/Fixture
                [weightage] => 29
                [ledgerName] => Water Coolers,Referigerator
            )  

    )    

)

Thanks in advance.

</div>

You could use the usort function, passing a callback, like this:

function compare($array1, $array2) {

    $a = $array1['weightage'];
    $b = $array2['weightage'];

    if ($a < $b) {
        return -1;
    }
    elseif($b < $a) {
        return 1;
    }

    return 0;
}

$result = usort($originalArray, "compare");

Thanks for reformating the code :) here is the usort method which does the job

$sortedArray = usort($originalArray, function($firstItem, $secondItem){
    return $firstItem['weightage'] < $secondItem['weightage'];
});

or if you don't prefer to use anonymous function :

function sortByWeightage($firstItem, $secondItem)
{
    return $firstItem['weightage'] < $secondItem['weightage'];
}

$sortedArray = usort($originalArray, "sortByWeihtage");

here is a functions snippet, using the provided sample data (well I've changed the 'weightage' values to demonstrate reordering, and applied the same to the question):

<?php

$originalArray = [
    'Furniture/Fixture' => [
        [
            'LedgerId' => '557af7adc3acc3ac2b8b4567',
            'groupName' => 'Furniture/Fixture',
            'weightage' => 29,
            'ledgerName' => 'Furniture & Fixture',
        ],
        [
            'LedgerId' => '558ffa02c3acc331258b4567',
            'groupName' => 'Furniture/Fixture',
            'weightage' => 28,
            'ledgerName' => 'Water Coolers,Referigerator',
        ],
    ],
    'Building' => [
        [
            'LedgerId' => '5586c5eec3acc3f42c8b4567',
            'groupName' => 'Building',
            'weightage' => 26,
            'ledgerName' => 'Main Building',
        ],
        [
            'LedgerId' => '5586c85cc3acc38e2e8b4567',
            'groupName' => 'Building',
            'weightage' => 22,
            'ledgerName' => 'Jr.School Building',
        ],
    ],
    'Vehicle' => [
        [
            'LedgerId' => '55898004c3acc38b758b4567',
            'groupName' => 'Vehicle',
            'weightage' => 37,
            'ledgerName' => 'School Bus',
        ],
        [
            'LedgerId' => '55898078c3acc3c5758b4567',
            'groupName' => 'Vehicle',
            'weightage' => 27,
            'ledgerName' => 'Staff Car - TATA Mobile',
        ],
    ],
    'Plant & Machinery' => [
        [
            'LedgerId' => '557af81fc3acc3ed2b8b4567',
            'groupName' => 'Plant & Machinery',
            'weightage' => 18,
            'ledgerName' => 'Tools For Carpentres',
        ],
        [
            'LedgerId' => '557afff7c3acc38d2e8b4567',
            'groupName' => 'Plant & Machinery',
            'weightage' => 29,
            'ledgerName' => 'Water Heating Equipment',
        ],
        [
            'LedgerId' => '557b004dc3acc3bf2e8b4567',
            'groupName' => 'Plant & Machinery',
            'weightage' => 28,
            'ledgerName' => 'Mess Cold Room',
        ],
    ],
];

echo 'Printing original array BEFORE being sorted' . PHP_EOL;
print_r($originalArray);

foreach ($originalArray as &$group) {

    $sortedGroup = usort($group, function($firstItem, $secondItem){
        return $firstItem['weightage'] > $secondItem['weightage'];
    });

}

echo 'Printing original array AFTER being sorted' . PHP_EOL;
print_r($originalArray);

UPDATE:

If you need to order groups and not group elements (as per our conversation bellow) then you could remove the loop and amend the usort as followed:

$sortedGroup = usort($originalArray, function($firstItem, $secondItem){
    return $firstItem[0]['weightage'] > $secondItem[0]['weightage'];
});  

But again as suggested earlier its not the best data structure to have the group order in all of the groups element, if you have control over it consider to reformat it to something like :

$originalArray = [
    'Furniture/Fixture' => [
        'weightage' => 100,
        'items' => [
            [
                'LedgerId' => '557af7adc3acc3ac2b8b4567',
                'groupName' => 'Furniture/Fixture',
                'ledgerName' => 'Furniture & Fixture',
            ],
            [
                'LedgerId' => '558ffa02c3acc331258b4567',
                'groupName' => 'Furniture/Fixture',
                'ledgerName' => 'Water Coolers,Referigerator',
            ],
        ]
    ],
......

and use a unique specific key for sorting them