简体   繁体   English

使用子值上的动态键对多维数组进行排序 - PHP

[英]Sort Multidimensional Array with Dynamic Key on Child Value - PHP

I have a 4 dimensional associative array, with all of its key dynamic.我有一个 4 维关联数组,其所有关键动态。 I want to sort the array on the value of its last child.我想根据最后一个孩子的值对数组进行排序。 For a reference, this is the array:作为参考,这是数组:

$array = array(
    "Africa" => array(
        "Egypt" => array(
            "20" => array(
                "basicInfo" => array('population' => 'xxx', 'size' => 'xxx')
            )
        ),
        "Sudan" => array(
            "249" => array(
                "basicInfo" => array('population' => 'xxx', 'size' => 'xxx')
            )
        ),
        ...
        ...
    ),
    "Europe" => array(
        "Greece" => array(
            "30" => array(
                "basicInfo" => array('population' => 'xxx', 'size' => 'xxx')
            )
        ),
        "Netherlands" => array(
            "31" => array(
                "basicInfo" => array('population' => 'xxx', 'size' => 'xxx')
            )
        ),
        ...
        ...
    ),
    ...
    ...
);

In the above array, I want to sort on the basis of population in descending order.在上面的数组中,我想根据人口降序排序。 So far, I've tried uasort , array_multisort , but I think I am doing it wrong.到目前为止,我已经尝试过uasortarray_multisort ,但我认为我做错了。 Can someone please help me sort this?有人可以帮我整理一下吗?

uasort($records[$continent][$country][$countryCode]['basicInfo'], array($this, 'sortByPopulation' ));

To be clear, I am sorting it in the for loop while it is being created.需要明确的是,我正在创建它时在 for 循环中对其进行排序。 Since all the variables inside the array are dynamic, I think it cannot be sorted after generating the array.由于数组内部的所有变量都是动态的,所以我认为生成数组后是无法排序的。

The final result should be in the same format as above, continents > countries > codes > basicInfo.最终结果应该和上面的格式一样,大洲>国家>代码>基本信息。

Thank You谢谢你

Assuming you want to sort within continents, this code will do what you want.假设您想在大洲内排序,此代码将执行您想要的操作。 It iterates over each continent within the array, using a custom sort function to sort on the population value for each country.它遍历数组中的每个大陆,使用自定义排序函数对每个国家的population值进行排序。 To deal with the dynamic keys within the country, we use current to get the first element of the array, from which we can then get to basicInfo and then population :为了处理国内的动态key,我们使用current来获取数组的第一个元素,然后我们可以从中获取basicInfo ,然后population

$array = array_map(function ($arr) {
    uasort($arr, function ($a, $b) {
        return current($b)['basicInfo']['population'] - current($a)['basicInfo']['population'];
    });
    return $arr;
}, $array);
print_r($array);

Demo on 3v4l.org 3v4l.org 上的演示

Note that it doesn't make any sense to sort while you are generating the array as you will still need to sort it after you insert the last value.请注意,在生成数组时进行排序没有任何意义,因为在插入最后一个值后仍需要对其进行排序。 So just run your loop and then sort...所以只需运行你的循环然后排序......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM