1

I would like to know how to return a value of an object with a name value pair that's inside an array. I've been trying all sorts of methods and frankly I realized I may be way over my head on this. I'd like some assistance trying to get the AirportsInformation_DataExtension value inside the property array.

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
                                )

                        )

                )

        )

)

Using a foreach loop I was able to print out all lines with name/value sets

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

Sadly I can't get passed that. I just need to retrieve the value. Thanks in advance.

2
  • If the $results is that object you outputted above, then your code should work. What error are you getting (assuming you have error reporting on). Commented Jan 16, 2013 at 9:19
  • Hi Glavic, with the foreach loop there was no error generated. Commented Jan 16, 2013 at 19:55

4 Answers 4

2

In order to get the value, you could loop over them and test for the name matching AirportsInformation_DataExtension:

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

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

}
Sign up to request clarification or add additional context in comments.

Comments

0

If you would need to be able to get all the values based on their name, it could be useful to turn it in to an associative array, like this

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

Then you can get the values later by indexing them directly, i.e.

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

Comments

0

Use the above solution by MrCode or, simply just use $results->Results->Properties->Property[15]->Value if the index of AirportsInformation_DataExtension is always 15. Since an array is an ordered list, it is very probably that the index does not change unless some of the items are removed/added from the array/

1 Comment

Hi @Sparky, this also works beautifully. There's low chance that the index will change. Thanks!
0
foreach ($results->Results->Properties->Property as $CurrentProp){
    $tempArr[$CurrentProp->Name] = $CurrentProp->Value;
}

echo $tempArr['AirportsInformation_DataExtension'];

By this you can access any other key of that object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.