PHP - 对象和数组 - 如何访问数组中的 stdClass 对象"name" "value"对?



我想知道如何使用数组中的名称-值对返回对象的值。我一直在尝试各种方法,坦率地说,我意识到我在这方面可能有点过头了。我希望在尝试获取属性数组中的AirportsInformation_DataExtension值时获得一些帮助。

stdClass Object
(
    [OverallStatus] => OK
    [RequestID] => 19e41b46-df68-47ba-8858-d728f3a92036
    [Results] => stdClass Object
        (
            [PartnerKey] => 
            [ObjectID] => 
            [Type] => DataExtensionObject
            [Properties] => stdClass Object
                (
                    [Property] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [Name] => CampaignName
                                    [Value] => 20130107_FlightDealsHotelPricePoints
                                )
                            [1] => stdClass Object
                                (
                                    [Name] => StartDate
                                    [Value] => 1/7/2013 12:00:00 AM
                                )
                            [2] => stdClass Object
                                (
                                    [Name] => EndDate
                                    [Value] => 1/15/2013 5:59:59 AM
                                )
                            [3] => stdClass Object
                                (
                                    [Name] => CampaignType
                                    [Value] => FlightDeals
                                )
                            [4] => stdClass Object
                                (
                                    [Name] => LandingPage_ExpireDate
                                    [Value] => 1/15/2013 5:59:59 AM
                                )
                            [5] => stdClass Object
                                (
                                    [Name] => LandingPage_AutoRedirectOnExpire
                                    [Value] => True
                                )
                            [6] => stdClass Object
                                (
                                    [Name] => LandingPage_ExpireTargetURL
                                    [Value] => test
                                )
                            [7] => stdClass Object
                                (
                                    [Name] => BookByDate
                                    [Value] => 1/22/2013 12:00:00 AM
                                )
                            [8] => stdClass Object
                                (
                                    [Name] => TravelStartDate
                                    [Value] => 
                                )
                            [9] => stdClass Object
                                (
                                    [Name] => TravelEndDate
                                    [Value] => 
                                )
                            [10] => stdClass Object
                                (
                                    [Name] => FlightDeals_DataExtension
                                    [Value] => 20130107_DestinationFlightDeals
                                )
                            [11] => stdClass Object
                                (
                                    [Name] => FlightDeals_SortOrder_DataExtension
                                    [Value] => FlightDeals_DestinationSortOrder
                                )
                            [12] => stdClass Object
                                (
                                    [Name] => HotelDeals_DataExtension
                                    [Value] => 20130107_FlightDealsHotelPricePoints
                                )
                            [13] => stdClass Object
                                (
                                    [Name] => HotelDeals_All_DataExtension
                                    [Value] => 20130107_HotelPackageDeals_ALL
                                )
                            [14] => stdClass Object
                                (
                                    [Name] => HotelInformation_DataExtension
                                    [Value] => EmailHotelInformation
                                )
                            [15] => stdClass Object
                                (
                                    [Name] => AirportsInformation_DataExtension
                                    [Value] => Airports
                                )
                            [16] => stdClass Object
                                (
                                    [Name] => RoutesInformation_DataExtension
                                    [Value] => Routes
                                )
                            [17] => stdClass Object
                                (
                                    [Name] => DFP_DataExtension
                                    [Value] => ET_DestinationIframeSrc
                                )
                            [18] => stdClass Object
                                (
                                    [Name] => DeepLinkConnectorURL
                                    [Value] => http://www.somewebsite/BookingConnector.html?mode=run
                                )
                            [19] => stdClass Object
                                (
                                    [Name] => DefaultDestinationScenery
                                    [Value] => LAS
                                )
                            [20] => stdClass Object
                                (
                                    [Name] => DefaultHomeAirportCode
                                    [Value] => 
                                )
                            [21] => stdClass Object
                                (
                                    [Name] => FailSafeHomeAiportCode
                                    [Value] => 
                                )
                            [22] => stdClass Object
                                (
                                    [Name] => DFP_Campaign_Banner
                                    [Value] => True
                                )
                            [23] => stdClass Object
                                (
                                    [Name] => EmailID
                                    [Value] => 44388
                                )
                        )
                )
        )
)

使用foreach循环,我可以打印出所有带有名称/值集的行

foreach ($results->Results->Properties->Property as $CurrentProp){
    print('<br>');
    print('Name: '.$CurrentProp->Name. ' Value: '.$CurrentProp->Value.'<br>');                    
};

遗憾的是,我不能通过。我只需要检索值。提前谢谢。

为了获得值,您可以对它们进行循环并测试名称匹配AirportsInformation_DataExtension:

foreach ($results->Results->Properties->Property as $CurrentProp){
    if($CurrentProp->Name == 'AirportsInformation_DataExtension')
    {
        echo 'The value is: ' . $CurrentProp->Value;
    }
}

如果您需要能够根据值的名称获取所有值,那么将其转换为关联数组可能会很有用,比如这个

$results->Results->Properties->PropertyArray = array();
foreach($results->Results->Properties->Property as $arrCurrentProperty) {
  $results->Results->Properties->PropertyArray[$arrCurrentProperty->Name] = $arrCurrentProperty->Value;
};

然后,您可以稍后通过直接对其进行索引来获得值,即

echo 'The value is: ' . $results->Results->Properties->PropertyArray['AirportsInformation_DataExtension'];

使用MrCode的上述解决方案,或者,如果AirportsInformation_DataExtension的索引始终为15,则仅使用$results->Results->Properties->Property[15]->Value。由于数组是ordered列表,除非从数组/

中删除/添加某些项,否则索引很可能不会更改
foreach ($results->Results->Properties->Property as $CurrentProp){
    $tempArr[$CurrentProp->Name] = $CurrentProp->Value;
}
echo $tempArr['AirportsInformation_DataExtension'];

通过此操作,您可以访问该对象的任何其他键。

最新更新