Best way to access properties deep in an Object

Hi,

I am Wondering what the best way to access properties deep in object?

for example below i have an object that gets returned from an API

The information i want to pick out is the Account name ( In [B] tags ) and then all the product codes listed under that (also in [B] tags). I was able to do it with loops inside loops (like 3 internal loops :x ) But think there may be a more efficent way.

object QueryResult(4) {
    public queryLocator => NULL
    public done => bool TRUE
    public records => array(2) (
        0 => object SObject(4) {
            public type => string(11) "Opportunity"
            public fields => object stdClass(3) {
                public Name => string(8) "Test Opp"
                public Id => string(18) "0063000000mSTvhAAG"
                public Account => object SObject(2) {
                    public type => string(7) "Account"
                    public fields => object stdClass(1) {
                        public [B]Name[/B] => string(16) "Some Account"
                    }
                }
            }
            public Id => string(18) "0063000000mSTvhAAG"
            public queryResult => array(1) (
                0 => object QueryResult(4) {
                    public queryLocator => NULL
                    public done => bool TRUE
                    public records => array(3) (
                        0 => object SObject(2) {
                            public type => string(19) "OpportunityLineItem"
                            public fields => object stdClass(2) {
                                public Quantity => string(3) "1.0"
                                public PricebookEntry => object SObject(2) {
                                    public type => string(14) "PricebookEntry"
                                    public fields => object stdClass(1) {
                                        public [B]ProductCode[/B] => string(5) "12345"
                                    }
                                }
                            }
                        }
                        1 => object SObject(2) {
                            public type => string(19) "OpportunityLineItem"
                            public fields => object stdClass(2) {
                                public Quantity => string(3) "1.0"
                                public PricebookEntry => object SObject(2) {
                                    public type => string(14) "PricebookEntry"
                                    public fields => object stdClass(1) {
                                        public [B]ProductCode[/B] => string(6) "4565"
                                    }
                                }
                            }
                        }
                        2 => object SObject(2) {
                            public type => string(19) "OpportunityLineItem"
                            public fields => object stdClass(2) {
                                public Quantity => string(3) "1.0"
                                public PricebookEntry => object SObject(2) {
                                    public type => string(14) "PricebookEntry"
                                    public fields => object stdClass(1) {
                                        public [B]ProductCode[/B] => string(6) "23453"
                                    }
                                }
                            }
                        }
                    )
                    public size => integer 3
                }
            )
        }
        1 => object SObject(4) {
            public type => string(11) "Opportunity"
            public fields => object stdClass(3) {
                public Name => string(36) "Opp Name"
                public Id => string(18) "0063000000mSVpQAAW"
                public Account => object SObject(2) {
                    public type => string(7) "Account"
                    public fields => object stdClass(1) {
                        public Name => string(16) "Some Account"
                    }
                }
            }
            public Id => string(18) "0063000000mSVpQAAW"
            public queryResult => array(1) (
                0 => object QueryResult(4) {
                    public queryLocator => NULL
                    public done => bool TRUE
                    public records => array(1) (
                        0 => object SObject(2) {
                            public type => string(19) "OpportunityLineItem"
                            public fields => object stdClass(2) {
                                public Quantity => string(3) "1.0"
                                public PricebookEntry => object SObject(2) {
                                    public type => string(14) "PricebookEntry"
                                    public fields => object stdClass(1) {
                                        public [B]ProductCode[/B] => string(6) "56785"
                                    }
                                }
                            }
                        }
                    )
                    public size => integer 1
                }
            )
        }
    )
    public size => integer 2
}

Thanks in advanced!

Well, the only way i could do it is with 3 Foreach’s as well…

I forgot to update this, but i got it in 2 foreach loops :slight_smile:

		foreach ($records as $record) 
		{
			$sObject = new SObject($record);			
			
			foreach($sObject->queryResult[0]->records as $data)
			{
				
				echo $data->fields->PricebookEntry->fields->ProductCode . "<br />";
				
			}
			
		}

Thanks,

Ian

Well yeah, 2 if you know for a fact that queryResult will always be a 1-item array. You also probably want to scoop Name out in the first loop.