1

我想知道如何使用数组内的名称值对返回对象的值。我一直在尝试各种方法,坦率地说,我意识到我可能在这方面太过分了。我需要一些帮助来尝试在属性数组中获取 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>');                    
};

可悲的是我无法通过那个。我只需要检索值。提前致谢。

4

4 回答 4

2

为了获取值,您可以遍历它们并测试名称匹配AirportsInformation_DataExtension

foreach ($results->Results->Properties->Property as $CurrentProp){

    if($CurrentProp->Name == 'AirportsInformation_DataExtension')
    {
        echo 'The value is: ' . $CurrentProp->Value;
    }

}
于 2013-01-16T09:19:46.387 回答
0

使用 MrCode 的上述解决方案,或者只使用$results->Results->Properties->Property[15]->Valueif 的索引AirportsInformation_DataExtensionalways 15。由于数组是ordered列表,因此索引很可能不会更改,除非从数组中删除/添加某些项目/

于 2013-01-16T09:23:33.210 回答
0

如果您需要能够根据名称获取所有值,则将其转换为关联数组可能会很有用,如下所示

$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'];
于 2013-01-16T09:24:58.903 回答
0
foreach ($results->Results->Properties->Property as $CurrentProp){
    $tempArr[$CurrentProp->Name] = $CurrentProp->Value;
}

echo $tempArr['AirportsInformation_DataExtension'];

通过这个,您可以访问该对象的任何其他键。

于 2013-01-16T09:32:24.973 回答