使用数据库循环创建嵌套的多维数组

时间:2017-11-14 06:49:28

标签: php arrays multidimensional-array

我已经在这里工作了几天,几乎把头发拉了出来。

我正在尝试从FOREACH循环输出JSON,以便我可以使用它来模拟每日课程表。我已经有了大部分的方式,事情继续破裂或出乎意料。

以下代码产生了这个:

 {
"DAY": "TUESDAY",
"TIME": {
    "530": {
        "CLASSES": [
            {
                "CLASS": {
                    "INSTRUCTOR": "DANA",
                    "CLASS_LEVEL": "JUNIOR",
                    "MAX_STUDENTS": "8"
                }
            },
            {
                "CLASS": {
                    "STUDENTS": [
                        {
                            "STUDENT": [
                                {
                                    "FIRST NAME": "Alexandra",
                                    "LAST NAME": "",
                                    "DATE OF BIRTH": ""
                                }
                            ]
                        }
                    ]
                }
            },
            {
                "CLASS": {
                    "STUDENTS": [
                        {
                            "STUDENT": [
                                {
                                    "FIRST NAME": "Leivi",
                                    "LAST NAME": "",
                                    "DATE OF BIRTH": ""
                                }
                            ]
                        }
                    ]
                }
            },
            {
                "CLASS": {
                    "STUDENTS": [
                        {
                            "STUDENT": [
                                {
                                    "FIRST NAME": "Traivon",
                                    "LAST NAME": "",
                                    "DATE OF BIRTH": ""
                                }
                            ]
                        }
                    ]
                }
            },
            {
                "CLASS": {
                    "STUDENTS": [
                        {
                            "STUDENT": [
                                {
                                    "FIRST NAME": "Keira",
                                    "LAST NAME": "",
                                    "DATE OF BIRTH": ""
                                }
                            ]
                        }
                    ]
                }
            },
            {
                "CLASS": {
                    "STUDENTS": [
                        {
                            "STUDENT": [
                                {
                                    "FIRST NAME": "Madison",
                                    "LAST NAME": "",
                                    "DATE OF BIRTH": ""
                                }
                            ]
                        }
                    ]
                }
            },

但是,正如您所看到的,我希望“学生”嵌套在“CLASS”下面。我究竟做错了什么?我无法理解我的生活。我开始讨厌多维数组。 : - @

function dailySchedule() {
    global $conn;
    $sql = 'SET DATEFIRST 1

            SELECT [DAY].[DAY] AS [DAY], CLASS.CLASSTIME AS CLASSTIME, CLASSLEVEL.CLASSLEVEL AS CLASSLEVEL, CLASS.MAXSTUDENT AS MAXSTUDENT,
            INSTRUCTOR.FIRSTNAME AS INSTRUCTOR, STUDENT.FIRSTNAME AS STUDENTFIRST, STUDENT.SURNAME AS STUDENTLAST, STUDENT.DOB AS STUDENTDOB

            FROM STUDENT JOIN BOOKING ON STUDENT.ID = BOOKING.STUDENTID JOIN CLASS ON CLASS.ID = BOOKING.CLASSID JOIN CLASSLEVEL ON 
            CLASS.CLASSLEVELID = CLASSLEVEL.ID JOIN [DAY] ON CLASS.CLASSDAY = [DAY].ID JOIN INSTRUCTOR ON CLASS.INSTRUCTORID = INSTRUCTOR.ID

            WHERE   [DAY].ID = (DATEPART(dw, GETUTCDATE() AT TIME ZONE \'AUS Eastern Standard Time\'))

            ORDER BY CLASS.CLASSTIME ASC, INSTRUCTOR.FIRSTNAME ASC, CLASSLEVEL.CLASSLEVEL ASC';


    $studentDetails =
    $sqlEx = $conn->query($sql);
    $schedule = array();

    foreach($sqlEx as $rows) {

        if(!isset($day) || $day !== $rows['DAY']) {
            $day = $rows['DAY'];
            $schedule = array('DAY' => $day) + $schedule;
        }
        if(!isset($time) || $time !== $rows['CLASSTIME']) {
            $time = $rows['CLASSTIME'];
        }
        if(!isset($instructor) || $instructor !== $rows['INSTRUCTOR']) {
            $instructor = $rows['INSTRUCTOR'];
            $schedule['TIME'][$time]['CLASSES'][]['CLASS'] = [
                'INSTRUCTOR' => $instructor,
                'CLASS_LEVEL' => $rows['CLASSLEVEL'],
                'MAX_STUDENTS' => $rows['MAXSTUDENT'],
            ];
        }

        //if($instructor == $rows['INSTRUCTOR']){
            $schedule['TIME'][$time]['CLASSES'][]['CLASS']['STUDENTS'][]['STUDENT'][] = [
                    'FIRST NAME' => $rows['STUDENTFIRST'],
                    'LAST NAME' => $rows['STUDENTLAST'],
                    'DATE OF BIRTH' => $rows['STUDENTDOB']
                    ];


        //}

    }
    echo json_encode($schedule, JSON_PRETTY_PRINT);

}

先谢谢大家。

1 个答案:

答案 0 :(得分:-1)

我不确定这是你想要的,但我最喜欢的方法是使用不同的方法:

$schedule = array();

    foreach($sqlEx as $rows) {

       $Day = $rows["DAY"];
       $ClassTime = $rows["CLASSTIME"];
       $ClassLevel = $rows["CLASSLEVEL"];
       $MaxStudents = $rows["MAXSTUDENTS"];
       $Instructor = $rows["INSTRUCTOR"];
       $StudentFirst = $rows["STUDENTFIRST"];
       $StudentLast = $rows["STUDENTLAST"];
       $StudentDOB = $rows["STUDENTDOB"];


       $schedule[$Day][$ClassTime]["CLASS"] = array("INSTRUCTOR" => $Instructor,"CLASS_LEVEL" =>$ClassLevel,"MAX_SUTENTS" => $MaxStudents);
       $schedule[$Day][$ClassTime]["STUDENT"] = array("FIRSTNAME" => $StudentFirst ,"LASTNAME" => $StudentLast, "DATE OF BIRTH" => $StudentDOB);

    }

    echo json_encode($schedule, JSON_PRETTY_PRINT);