如何循环关联数组

时间:2014-06-11 09:37:42

标签: php arrays loops associative

$myArray = Array
    (
        [Header] => Array
            (
                [@attributes] => Array
                    (
                        [ShouldRecordPerformanceTime] => False
                        [Timestamp] => 2014-06-11 16:33:20:04501
                        [ReferenceID] => 8fc07483-94ff-4a70-a8fa-d54292598592
                        [RecentlyTime] => 2014-06-11 16:33:08
                        [AccessCount] => 30
                        [CurrentCount] => 2
                        [ResetTime] => 2014-06-11 16:34:08
                        [ResultCode] => Success
                    )

            )

        [HotelResponse] => Array
            (
                [OTA_HotelSearchRS] => Array
                    (
                        [@attributes] => Array
                            (
                                [TimeStamp] => 2014-06-11T16:33:19.8857903+08:00
                                [Version] => 1.0
                                [PrimaryLangID] => zh
                            )

                        [Properties] => Array
                            (
                                [Property] => Array
                                    (
                                        [@attributes] => Array
                                            (
                                                [BrandCode] => 110
                                                [HotelCode] => 443707
                                                [HotelCityCode] => 2
                                                [HotelName] => rujiakuaijie
                                                [AreaID] => 112
                                                [HotelId] => 443707
                                            )

                                        [VendorMessages] => Array
                                            (
                                            )

                                        [Position] => Array
                                            (
                                                [@attributes] => Array
                                                    (
                                                        [Latitude] => 31.235452
                                                        [Longitude] => 121.447776
                                                        [PositionTypeCode] => 502
                                                    )

                                            )

                                        [Address] => Array
                                            (
                                                [AddressLine] => changpin road
                                                [CityName] => SHANGHAI
                                                [PostalCode] => 200040
                                            )

                                        [Award] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [@attributes] => Array
                                                            (
                                                                [Provider] => HotelStarRate
                                                                [Rating] => 2
                                                            )

                                                    )

                                                [1] => Array
                                                    (
                                                        [@attributes] => Array
                                                            (
                                                                [Provider] => CtripStarRate
                                                                [Rating] => 2
                                                            )

                                                    )

                                                [2] => Array
                                                    (
                                                        [@attributes] => Array
                                                            (
                                                                [Provider] => CtripRecommendRate
                                                                [Rating] => 1.5
                                                            )

                                                    )

                                                [3] => Array
                                                    (
                                                        [@attributes] => Array
                                                            (
                                                                [Provider] => CtripCommRate
                                                                [Rating] => 0
                                                            )

                                                    )


                                            )

                                        [RelativePosition] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [@attributes] => Array
                                                            (
                                                                [Distance] => 1.58
                                                                [UnitOfMeasureCode] => 2
                                                                [Name] => changshou
                                                            )

                                                    )

                                                [1] => Array
                                                    (
                                                        [@attributes] => Array
                                                            (
                                                                [Distance] => 44.729
                                                                [UnitOfMeasureCode] => 2
                                                                [Name] => pudong
                                                            )

                                                    )

                                                [2] => Array
                                                    (
                                                        [@attributes] => Array
                                                            (
                                                                [Distance] => 12.88
                                                                [UnitOfMeasureCode] => 2
                                                                [Name] => hongqiao
                                                            )

                                                    )

                                                [3] => Array
                                                    (
                                                        [@attributes] => Array
                                                            (
                                                                [Distance] => 11.208
                                                                [UnitOfMeasureCode] => 2
                                                                [Name] => shanghai
                                                            )

                                                    )

                                                [4] => Array
                                                    (
                                                        [@attributes] => Array
                                                            (
                                                                [Distance] => 15.571
                                                                [UnitOfMeasureCode] => 2
                                                                [Name] => trainstation
                                                            )

                                                    )

                                                [5] => Array
                                                    (
                                                        [@attributes] => Array
                                                            (
                                                                [Distance] => 4.167
                                                                [UnitOfMeasureCode] => 2
                                                                [Name] => central
                                                            )

                                                    )

                                                [6] => Array
                                                    (
                                                        [@attributes] => Array
                                                            (
                                                                [Distance] => 3.836
                                                                [UnitOfMeasureCode] => 2
                                                                [Name] => museum
                                                            )

                                                    )





                                            )

                                        [TPA_Extensions] => Array
                                            (
                                                [Zone] => Array
                                                    (
                                                        [ZoneType] => Array
                                                            (
                                                                [@attributes] => Array
                                                                    (
                                                                        [ZoneID] => 981
                                                                        [ZoneName] => commercial
                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                            )

                        [Success] => Array
                            (
                            )

                    )

            )

    )

数组看起来像这样,我使用sample of php.net循环遍历数组以获取键和值 0:a 0:subA 1:subB 0:subsubA 1:subsubB 0:deepA 1:deepB

不幸的是我没有得到任何回应,任何人都可以提供帮助

$iterator = new RecursiveArrayIterator($myArray); 
iterator_apply($iterator, 'traverseStructure', array($iterator)); 

function traverseStructure($iterator) { 

    while ( $iterator -> valid() ) { 

        if ( $iterator -> hasChildren() ) { 

            traverseStructure($iterator -> getChildren()); 

        } 
        else { 
            echo $iterator -> key() . ' : ' . $iterator -> current() .PHP_EOL;    
        } 

        $iterator -> next(); 
    } 
} 
?> 

1 个答案:

答案 0 :(得分:0)

function DFS($array) {      
    foreach ($array as $k => $v) {  
        if (is_array($v)) {
            $this->DFS($v);
        } 
        else echo '<br />'. $k. ' - '. $v;
    }       
}