排序SimpleXML Array php

时间:2016-10-10 13:07:23

标签: php arrays simplexml ksort

我有以下XML文件timezone;我想按时区名称排序,但这也会对子类别进行排序。我只想对时区进行排序。

SimpleXMLElement Object
(
    [code] => CZ
    [timezone] => Europe/Prague
)
SimpleXMLElement Object
(
    [code] => AQ
    [timezone] => Antarctica/South_Pole
)
SimpleXMLElement Object
(
    [code] => BB
    [timezone] => America/Barbados
)
SimpleXMLElement Object
(
    [code] => IL
    [timezone] => Asia/Jerusalem
)
SimpleXMLElement Object
(
    [code] => BY
    [timezone] => Europe/Minsk
)
SimpleXMLElement Object
(
    [code] => WF
    [timezone] => Pacific/Wallis
)
SimpleXMLElement Object
(
    [code] => ST
    [timezone] => Africa/Sao_Tome
)
SimpleXMLElement Object
(
    [code] => ES
    [timezone] => Europe/Madrid
    [zones] => SimpleXMLElement Object
        (
            [zone] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [codigo] => 07
                            [timezone] => Europe/Madrid
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [codigo] => 27
                            [timezone] => Europe/Madrid
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [codigo] => 29
                            [timezone] => Europe/Madrid
                        )

                    [3] => SimpleXMLElement Object
                        (
                            [codigo] => 31
                            [timezone] => Europe/Madrid
                        )

                    [4] => SimpleXMLElement Object
                        (
                            [codigo] => 32
                            [timezone] => Europe/Madrid
                        )

                    [5] => SimpleXMLElement Object
                        (
                            [codigo] => 34
                            [timezone] => Europe/Madrid
                        )

                    [6] => SimpleXMLElement Object
                        (
                            [codigo] => 39
                            [timezone] => Europe/Madrid
                        )

                    [7] => SimpleXMLElement Object
                        (
                            [codigo] => 51
                            [timezone] => Africa/Ceuta
                        )

                    [8] => SimpleXMLElement Object
                        (
                            [codigo] => 52
                            [timezone] => Europe/Madrid
                        )

                    [9] => SimpleXMLElement Object
                        (
                            [codigo] => 53
                            [timezone] => Atlantic/Canary
                        )

                    [10] => SimpleXMLElement Object
                        (
                            [codigo] => 54
                            [timezone] => Europe/Madrid
                        )

                    [11] => SimpleXMLElement Object
                        (
                            [codigo] => 54
                            [timezone] => Europe/Madrid
                        )

                    [12] => SimpleXMLElement Object
                        (
                            [codigo] => 54
                            [timezone] => Europe/Madrid
                        )

                    [13] => SimpleXMLElement Object
                        (
                            [codigo] => 55
                            [timezone] => Europe/Madrid
                        )

                    [14] => SimpleXMLElement Object
                        (
                            [codigo] => 56
                            [timezone] => Europe/Madrid
                        )

                    [15] => SimpleXMLElement Object
                        (
                            [codigo] => 57
                            [timezone] => Europe/Madrid
                        )

                    [16] => SimpleXMLElement Object
                        (
                            [codigo] => 58
                            [timezone] => Europe/Madrid
                        )

                    [17] => SimpleXMLElement Object
                        (
                            [codigo] => 59
                            [timezone] => Europe/Madrid
                        )

                    [18] => SimpleXMLElement Object
                        (
                            [codigo] => 60
                            [timezone] => Europe/Madrid
                        )    
                )    
        )    
)
SimpleXMLElement Object
(
    [code] => FK
    [timezone] => Atlantic/Stanley
)
SimpleXMLElement Object
(
    [code] => CA
    [timezone] => America/Edmonton
    [zones] => SimpleXMLElement Object
        (
            [zone] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [codigo] => AB
                            [timezone] => America/Edmonton
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [codigo] => BC
                            [timezone] => America/Vancouver
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [codigo] => MB
                            [timezone] => America/Winnipeg
                        )

                    [3] => SimpleXMLElement Object
                        (
                            [codigo] => NB
                            [timezone] => America/Halifax
                        )

                    [4] => SimpleXMLElement Object
                        (
                            [codigo] => NL
                            [timezone] => America/St_Johns
                        )

                    [5] => SimpleXMLElement Object
                        (
                            [codigo] => NS
                            [timezone] => America/Halifax
                        )

                    [6] => SimpleXMLElement Object
                        (
                            [codigo] => NT
                            [timezone] => America/Yellowknife
                        )

                    [7] => SimpleXMLElement Object
                        (
                            [codigo] => NU
                            [timezone] => America/Rankin_Inlet
                        )

                    [8] => SimpleXMLElement Object
                        (
                            [codigo] => ON
                            [timezone] => America/Toronto
                        )

                    [9] => SimpleXMLElement Object
                        (
                            [codigo] => PE
                            [timezone] => America/Halifax
                        )

                    [10] => SimpleXMLElement Object
                        (
                            [codigo] => QC
                            [timezone] => America/Montreal
                        )

                    [11] => SimpleXMLElement Object
                        (
                            [codigo] => SK
                            [timezone] => America/Regina
                        )

                    [12] => SimpleXMLElement Object
                        (
                            [codigo] => YT
                            [timezone] => America/Whitehorse
                        )    
                )    
        )    
)
SimpleXMLElement Object
(
    [code] => KI
    [timezone] => Indian/Comoro
)

我的PHP程序是:

$xmltimeZones = simplexml_load_file(COREPATH.'includes/xml/inc.timezones.xml'); 
        $timeZonesObj = $xmltimeZones->xpath("//timezones/country");
        foreach ($timeZonesObj as $timeZone){
            if(isset($timeZone->zones) && $timeZone->zones !=''){
                foreach ($timeZone->zones->zone as $value) {
                    $SubZonas[(string)$value->codigo] = (string)$value->timezone;
                    sort($SubZonas);
                }
                $timeZones[(string)$timeZone->timezone] = $SubZonas;
            } else {
                $timeZones[(string)$timeZone->code] = (string)$timeZone->timezone;
            }
        }
        return $timeZones;
    }

0 个答案:

没有答案