从PHP中的std类对象和数组中获取值

时间:2017-05-19 06:52:26

标签: php arrays loops stdclass

我正在尝试使用PHP从这种类型的数组中获取值。如何从这个数组中访问主题名称。我可以在不使用2 foreach的情况下获得这些值吗? 注意:主题数组计数不固定

 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
                )

        )

)

1 个答案:

答案 0 :(得分:1)

你需要嵌套的foreach

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

    }

 }