0

I know there are so many solutions for multi-dimensional array access. But any of them didn't work for me.

I have this array and i need to access the name property by looping through the whole array. How do i access the Value 'TEXTPRINT' which is in the Name attribute?

Array
(
    [0] => Array
        (
            [ParentSurvey] => Array
                (
                    [attributes] => Array
                        (
                            [type] => SM_Survey__c
                            [url] => /services/data/v36.0/sobjects/SM_Survey__c/a181400000FHVy0AAH
                        )

                    [Id] => a181400000FHVy0AAH
                    [Company__c] => a0Qa000000ME65MEAT
                    [Type__c] => Company Critical Roles
                    [Company__r] => Array
                        (
                            [attributes] => Array
                                (
                                    [type] => Company__c
                                    [url] => /services/data/v36.0/sobjects/Company__c/a0Qa000000ME65MEAT
                                )

                            [Id] => a0Qa000000ME65MEAT
                            [Name] => TEXTPRINT
                        )

                )

            [DescList] => Array
                (
                    [0] => Array
                        (
                            [attributes] => Array
                                (
                                    [type] => SM_Survey_Detail__c
                                    [url] => /services/data/v36.0/sobjects/SM_Survey_Detail__c/a171400000PkHmsAAF
                                )

                            [Id] => a171400000PkHmsAAF
                            [CR_Designation__c] => a0ka0000004r6biAAA
                            [SM_Survey__c] => a181400000FHVy0AAH
                            [CR_Designation__r] => Array
                                (
                                    [attributes] => Array
                                        (
                                            [type] => Designation__c
                                            [url] => /services/data/v36.0/sobjects/Designation__c/a0ka0000004r6biAAA
                                        )

                                    [Id] => a0ka0000004r6biAAA
                                    [Name] =>Manager
                                )

                        )

I tried this code to print the value..

foreach ($result as $record){
    print_r($record['Array']['ParentSurvey']['Company__r']['Name']);
}
1
  • 1
    what foreach does is iterate over the parent array and return each child as it's own element. so the ['Array'] is not needed. also you should note the the Array from the print_r is a statement of type and not a usable key Commented May 23, 2017 at 9:57

1 Answer 1

1

Try this

foreach ($result as $record){
    print_r($record['ParentSurvey']['Company__r']['Name']);
}
Sign up to request clarification or add additional context in comments.

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.