根据键对多维关联数组进行排序

时间:2015-07-17 06:56:22

标签: php arrays sorting

嗨,我有一个如下数组:

cordova plugin add cordova-plugin-splashscreen

我想使用每个数组$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', ], ], ]; 中的键对每个关联数组进行排序,例如Furniture/FixtureBuildings。 我该如何解决它,以便输出看起来像这样?

weightage

提前致谢。

2 个答案:

答案 0 :(得分:0)

您可以使用 usort 功能,传递回调,如下所示:

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

答案 1 :(得分:0)

感谢您重新格式化代码:)这里是执行工作的usort方法

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

或者如果您不喜欢使用匿名功能:

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

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

这是一个功能代码段,使用提供的示例数据(我已经更改了&#39; weightage&#39;值以演示重新排序,并将相同的问题应用于此问题):

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

<强>更新

如果您需要订购群组而不是群组元素(根据我们的对话),您可以删除循环并修改usort,如下所示:

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

但是如前所述,不是最好的数据结构在所有组元素中都有组顺序,如果你可以控制它,可以考虑将其重新格式化为:

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

并使用唯一的特定键对其进行排序