2

次のデータを含む非常に大きな配列があります。

Array
(
    [2] => Array
        (
            [Berlin] => Array
                (
                    [1] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [name] => Joe
                                    [car_name] => Audi
                                    [car_color] => Black
                                    [sid] => 130 
                                )
                            [1] => stdClass Object
                                (
                                    [name] => Mark
                                    [car_name] => BMW
                                    [car_color] => Red
                                    [sid] => 135
                                )
                        )
                    [0] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [name] => Lucas
                                    [car_name] => Audi
                                    [car_color] => Yellow
                                    [sid] => 168
                                )
                            [1] => stdClass Object
                                (
                                    [name] => Joe
                                    [car_name] => Volkswagen
                                    [car_color] => Black
                                    [sid] => 170 
                                )
                            [2] => stdClass Object
                                (
                                    [name] => Thomas
                                    [car_name] => Ford
                                    [car_color] => Gray
                                    [sid] => 119
                                )
                        )
                )
            [Moscow] => Array
                (
                    [1] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [name] => Matt
                                    [car_name] => Mustang
                                    [car_color] => Black
                                    [sid] => 230 
                                )
                            [1] => stdClass Object
                                (
                                    [name] => Suze
                                    [car_name] => Lada
                                    [car_color] => Red
                                    [sid] => 245
                                )
                        )
                    [0] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [name] => Lucas
                                    [car_name] => Unknown
                                    [car_color] => Brown
                                    [sid] => 374
                                )
                            [1] => stdClass Object
                                (
                                    [name] => Mathew
                                    [car_name] => Volkswagen
                                    [car_color] => Blue
                                    [sid] => 589 
                                )
                            [2] => stdClass Object
                                (
                                    [name] => Thomas
                                    [car_name] => Ford
                                    [car_color] => Light Blue
                                    [sid] => 741
                                )
                        )
                )
        )
)

今、私はそれを次のように並べ替えたいと思います(//行を読んでください):

Array
(
    [2] => Array //from min to max - eg. from 0 to 50
        (
            [Berlin] => Array //alphabetically from A to Z
                (
                    [1] => Array //from min to max eg. from 0 to 1
                        (
                        )
                 )
         )
)

PHPでこれを達成するにはどうすればよいですか? 追加情報については、コメントでお気軽にお問い合わせください。

4

2 に答える 2

5

次のようになります:

// ksort sorts the keys low to high

ksort($array[2]);

// uksort w/ natsort is a full alphanumeric sorting A-Z0-9
uksort($array[2]['Berlin'], 'natsort');

ksort($array[2]['Berlin'][1]);

配列および配列関数に関するドキュメント:

http://us3.php.net/manual/en/book.array.php

機能例

この例は、実際にはあなたの例に固有のものです。ただし、配列構造が本当に動的である場合は、再帰的なスタイル関数を作成できます。

$BigArray = array();

//Sorting the initial array index
ksort($BigArray);

foreach($BigArray as $a)
{
    // Sorting Berlin / Moscow here..
    uksort($a, 'natsort');
    foreach($a as $b)
    {
        // Sorting the index inside the cities
        ksort($b);
    }
}
于 2012-11-28T19:57:03.397 に答える