简体   繁体   English

PHP按字母顺序排序

[英]PHP sort array alphabetically

I'm struggling on this one. 我在这个上苦苦挣扎。 I have an array that contains countries and regions. 我有一个包含国家和地区的数组。 I want to sort both sets of information in ascending order on the key. 我想在密钥上按升序对两组信息进行排序。

Here is the array I'm working with: 这是我正在使用的数组:

Array
(
    [Country] => Array
        (
            [United Kingdom] => Array
                (
                    [London] => Array
                        (
                            [0] => 1
                            [1] => 5
                            [2] => 23
                            [3] => 71
                        )

                    [Manchester] => Array
                        (
                            [0] => 800
                        )

                )

            [United States] => Array
                (
                    [New York] => Array
                        (
                            [0] => 147
                            [1] => 111
                        )

                    [Washington] => Array
                        (
                            [0] => 213
                        )

                    [Florida] => Array
                        (
                            [0] => 6
                        )

                    [Texas] => Array
                        (
                            [0] => 9
                        )

                )

            [Brazil] => Array
                (
                    [Brasília] => Array
                        (
                            [0] => 64
                        )

                )

        )

)

So the reordered array would be: 所以重新排序的数组将是:

Brazil 巴西
- Brasília - 巴西利亚

United Kingdom 英国
- London - 伦敦
- Manchester - 曼彻斯特

United States 美国
- Florida - 佛罗里达州
- New York - 纽约
- Texas - 德克萨斯州
- Washington - 华盛顿

The data structure should remain the same, but the order of the number (eg London: 1,5,23,71) can stay the same. 数据结构应保持不变,但数字的顺序(例如伦敦:1,5,23,71)可以保持不变。

I've tried several of the sorting methods from: http://php.net/manual/en/array.sorting.php 我已经尝试了几种排序方法: http//php.net/manual/en/array.sorting.php

But they dont appear to do anything. 但他们似乎没有做任何事情。 Maybe because its a multidimensional array or maybe its not structured 100% logically... but I'm stuck with the array as it is. 也许是因为它是一个多维数组,或者它的逻辑结构不是100%...但我坚持使用数组。

You can try: 你可以试试:

ksort_recursive($data);
print_r($data);

Function Used 使用的功能

function ksort_recursive(&$array) {
    ksort($array);
    foreach ( $array as &$a ) {
        is_array($a) && ksort_recursive($a);
    }
}

See Testing on Multiple PHP Versions 请参阅测试多个PHP版本

Step 1: 步骤1:
Sort the country by key. 按键对国家排序。

ksort($arr['Country']);

Step 2: Loop through the countries and sort those keys. 第2步:遍历各个国家/地区并对这些键进行排序。

foreach ($arr['Country'] as $country=>$data) {
    ksort($arr['Country'][$country]);
}

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

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