0

I'm trying to get the values from this type of array by using PHP. how to access the subject names from this array.?? can I get those values without using 2 foreach's ?? note: subject array count are not fixed

 stdClass Object
(
    [Examination] => Array
        (
            [0] => stdClass Object
                (
                    [ActivityID] => 734
                    [ActivityName] => EXECUTIVE LEVEL - II
                    [CloseDate] => 2017-10-02T00:00:00
                    [ExamSubjects] => stdClass Object
                        (
                            [ExamSubject] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [SubjectCode] => KE-2
                                            [SubjectID] => 001013
                                            [SubjectName] => KE2-Management Accounting Information
                                        )

                                    [1] => stdClass Object
                                        (
                                            [SubjectCode] => KE-3-LF
                                            [SubjectID] => 001016
                                            [SubjectName] => KE3B-Fundamentals of Law
                                        )

                                )

                        )

                    [OpenDate] => 2017-05-18T00:00:00
                    [SessionID] => 000091
                    [SessionName] => TestOnlineApp
                )

            [1] => stdClass Object
                (
                    [ActivityID] => 735
                    [ActivityName] => EXECUTIVE LEVEL - I & II
                    [CloseDate] => 2017-10-02T00:00:00
                    [ExamSubjects] => stdClass Object
                        (
                            [ExamSubject] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [SubjectCode] => KE-1
                                            [SubjectID] => 001012
                                            [SubjectName] => KE1-Financial Accounting & Reporting Fundamentals
                                        )

                                    [1] => stdClass Object
                                        (
                                            [SubjectCode] => KE-2
                                            [SubjectID] => 001013
                                            [SubjectName] => KE2-Management Accounting Information
                                        )


                                )

                        )

                    [OpenDate] => 2017-05-18T00:00:00
                    [SessionID] => 000091
                    [SessionName] => TestOnlineApp
                )

        )

)
5
  • If you want to access "everything" within this object, you will have to use 2 foreach loops. Can ExamSubjects contain multiple values? E.g. other than ExamSubject. Commented May 19, 2017 at 6:55
  • It actually depends on what you want to do. Nested foreach loops are an option, but it depends on what and how you need it. Having this a (Simple) XML document is probably more intuitive to handle from the structure of the data. Commented May 19, 2017 at 6:58
  • @TobiasF. please can you explain how to do that?? Commented May 19, 2017 at 7:01
  • @hakre please can you explain how to do that?? former question is not same like this, this is dynamic array Commented May 19, 2017 at 7:02
  • 1
    No need to get pushy and ping everyone, there is an in depth explanation in the linked duplicate even containing further links. There is even a search function on this website, we have tons of material regarding looping over an array. If you want to refer to the official documentation, perhaps php.net/foreach is a good start. A key search term might be multi-dimensional array - leading to: stackoverflow.com/q/40078683/367456, stackoverflow.com/q/32839554/367456 (just two examples). Commented May 19, 2017 at 7:05

1 Answer 1

1

You need nested foreach

foreach($data->Examination as $row)
{
    foreach($row->ExamSubjects->ExamSubject as $row1)
    {
        echo $row1->SubjectName

    }

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.