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
                )

        )

)
Sampath Wijesinghe
  • 789
  • 1
  • 11
  • 33
  • 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`. – Tobias F. May 19 '17 at 06: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. – hakre May 19 '17 at 06:58
  • @TobiasF. please can you explain how to do that?? – Sampath Wijesinghe May 19 '17 at 07:01
  • @hakre please can you explain how to do that?? former question is not same like this, this is dynamic array – Sampath Wijesinghe May 19 '17 at 07: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 https://php.net/foreach is a good start. A key search term might be *multi-dimensional array* - leading to: http://stackoverflow.com/q/40078683/367456, http://stackoverflow.com/q/32839554/367456 (just *two* examples). – hakre May 19 '17 at 07:05

1 Answers1

1

You need nested foreach

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

    }

 }
JYoThI
  • 11,977
  • 1
  • 11
  • 26