我的循环创建另一个父数组而不是将它附加到我指向的位置?

时间:2017-11-16 03:38:39

标签: php arrays multidimensional-array

我需要遍历我的数据库,并能够在日,时,班和学生之间进行分组。我已经能够使用一堆if(!isset())语句来实现我对此代码的理解。

foreach ($sqlEx as $rows) {
    if(!isset($day) || $day !== $rows['DAY']) {
        $day = $rows['DAY'];
        $scheduleJSON['DAY'] = $day;
    }
    if(!isset($time) || $time !== $rows['CLASSTIME']) {
        $time = $rows['CLASSTIME'];
        $scheduleJSON[$time]= [];
        $scheduleJSON[NULL] = $scheduleJSON[$time];
        unset($scheduleJSON[NULL]);
    }
    if(!isset($instructor) || $instructor !== $rows['INSTRUCTOR']) {
        $instructor = $rows['INSTRUCTOR'];
        $classlevel = $rows['CLASSLEVEL'];
        $maxstudent = $rows['MAXSTUDENT'];
        $scheduleJSON[$time]['CLASSES'][]['CLASS'] = ['INSTRUCTOR' => $instructor, 'CLASS LEVEL' => $classlevel, 'MAX STUDENTS' => $maxstudent];
    } else {
        $studentfirst = $rows['STUDENTFIRST'];
        $studentlast = $rows['STUDENTLAST'];
        $scheduleJSON[$time]['CLASSES']['CLASS']['STUDENTS'][] = ['STUDENT' => ['FIRST NAME' => $studentfirst, 'LAST NAME' => $studentlast]];
    }
}

我正在寻找让我的阵列具有这种结构:

$scheduleJSON = [
    'DAY' => NULL, 'TIME' => [
        'CLASSES' => [
            'CLASS' => [
                'INSTRUCTOR' => NULL, 'CLASS LEVEL' => NULL, 'MAX STUDENTS' => NULL, 'STUDENTS' => [
                    'STUDENT' => [
                        'FIRST NAME' => NULL, 'LAST NAME' => NULL, 'DATE OF BIRTH' => NULL, 'PARENT FIRST NAME' => NULL, 'PARENT LAST NAME' => NULL
                    ]
                ]
            ]
        ]
    ]
];

但是,当我打印到JSON编码时,我认为我的问题在我的else语句中。而不是在key => value之后将学生作为另一个MAX STUDENTS添加到课程中,而是创建另一个CLASS数组。

"DAY": "THURSDAY",
"530": {
    "CLASSES": {
        "0": {
            "CLASS": {
                "INSTRUCTOR": "Teacher",
                "CLASS LEVEL": "JUNIOR",
                "MAX STUDENTS": "8"
            }
        },
        "CLASS": {
            "STUDENTS": [
                {
                    "STUDENT": {
                        "FIRST NAME": "Student",
                        "LAST NAME": ""
                    }
                },

在这一点上的任何帮助将不胜感激 - 我一直在搜索问题并回答几天。

- 编辑 来自$ sqlEx的数组。请注意,我已删除了随附隐私的名称数据。

array(20) {
  ["DAY"]=>
  string(8) "THURSDAY"
  [0]=>
  string(8) "THURSDAY"
  ["CLASSTIME"]=>
  string(3) "530"
  [1]=>
  string(3) "530"
  ["CLASSLEVEL"]=>
  string(6) "JUNIOR"
  [2]=>
  string(6) "JUNIOR"
  ["MAXSTUDENT"]=>
  string(1) "8"
  [3]=>
  string(1) "8"
  ["INSTRUCTOR"]=>
  string(4) "TEACHER"
  [4]=>
  string(4) "TEACHER"
  ["STUDENTFIRST"]=>
  string(5) "First Name"
  [5]=>
  string(5) "First Name"
  ["STUDENTLAST"]=>
  string(0) ""
  [6]=>
  string(0) ""
  ["STUDENTDOB"]=>
  string(10) "2007-04-28"
  [7]=>
  string(10) "2007-04-28"
  ["PARENTFIRST"]=>
  string(12) "Parent First Name"
  [8]=>
  string(12) "Parent First Name"
  ["PARENTLAST"]=>
  string(6) "Parent Last Name"
  [9]=>
  string(6) "Parent Last Name"
}

2 个答案:

答案 0 :(得分:3)

首先,在添加INSTRUCTOR密钥时,您必须删除[]['CLASSES']之间的['CLASS']

$scheduleJSON[$time]['CLASSES']['CLASS'] = ['INSTRUCTOR' => $instructor, 'CLASS LEVEL' => $classlevel, 'MAX STUDENTS' => $maxstudent];

接下来,将所有内容移到其他条件之外。基本上删除了其他条件。

if(!isset($instructor) || $instructor !== $rows['INSTRUCTOR']) {
    // instructor codes
} // else is removed
$studentfirst = $rows['STUDENTFIRST'];
$studentlast = $rows['STUDENTLAST'];
$scheduleJSON[$time]['CLASSES']['CLASS']['STUDENTS'][] = ['STUDENT' => ['FIRST NAME' => $studentfirst, 'LAST NAME' => $studentlast]];

这将有效地工作,假设STUDENTFIRST到PARENTLAST的每个数据都是唯一的。没有重复的学生信息。否则,您需要检查学生是否已经存在。

注意:我建议删除else条件,因为它不会覆盖第一个循环。因此,第一行的学生信息不会被添加到$ scheduledJSON。 如果您需要else条件,则必须在$讲师的IF条件中添加学生信息

答案 1 :(得分:0)

因此在解决了@Erwin的问题之后,我终于能够输出JSON,如下所示:

{
"DAY": "THURSDAY",
"530": {
    "CLASSES": {
        "CLASS-1335": {
            "INSTRUCTOR": "TEACHER",
            "CLASS LEVEL": "JUNIOR",
            "MAX STUDENTS": "8",
            "START TIME": "530",
            "FINISH TIME": "730",
            "STUDENTS": [
                {
                    "STUDENT": {
                        "FIRST NAME": "First Name",
                        "LAST NAME": ""
                    }
                }
            ]
        },
        "CLASS-2243": {
            "INSTRUCTOR": "TEACHER",
            "CLASS LEVEL": "SENIOR ",
            "MAX STUDENTS": "8",
            "START TIME": "530",
            "FINISH TIME": "730",
            "STUDENTS": [
                {
                    "STUDENT": {
                        "FIRST NAME": "First Name",
                        "LAST NAME": ""
                    }
                },
                {
                    "STUDENT": {
                        "FIRST NAME": "First Name",
                        "LAST NAME": ""
                    }
                }, ETC...

以下是允许我完成此任务的代码。我需要在我的代码中添加一些逻辑来为每个类(例如CLASS.ID)提取唯一标识符,而不是盲目地向数组添加新级别。

因此,对于此多维数组的每个级别,需要if(!isset($var) || $var !== $rows[$val]) {}(按父对子的顺序)来辨别正在处理的当前记录是否具有相同的属性(例如DAY或CLASS TIME) - 如果任何语句中的值都不同(或未设置),设置新的变量值并将其推入数组。

foreach ($sqlEx as $rows) {
        if(!isset($day) || $day !== $rows['DAY']) {
            $day = $rows['DAY'];
            $scheduleJSON['DAY'] = $day;
        }
        if(!isset($time) || $time !== $rows['CLASSTIME']) {
            $time = $rows['CLASSTIME'];
            $scheduleJSON[$time]= [];
            $scheduleJSON[NULL] = $scheduleJSON[$time];
            unset($scheduleJSON[NULL]);
        }
        if(!isset($classid) || $classid !== $rows['CLASSID']) {
            $classid = $rows['CLASSID'];
            $instructor = $rows['INSTRUCTOR'];
            $classlevel = $rows['CLASSLEVEL'];
            $maxstudent = $rows['MAXSTUDENT'];
            $finishtime = $rows['CLASSENDTIME'];
            $scheduleJSON[$time]['CLASSES']['CLASS' . '-' . $classid] = [
                'INSTRUCTOR' => $instructor,
                'CLASS LEVEL' => $classlevel,
                'MAX STUDENTS' => $maxstudent,
                'START TIME' => $time,
                'FINISH TIME' => $finishtime
            ];
        }

        $studentfirst = $rows['STUDENTFIRST'];
        $studentlast = $rows['STUDENTLAST'];
        $scheduleJSON[$time]['CLASSES']['CLASS' . '-' . $classid]['STUDENTS'][] = [
            'STUDENT' => [
                'FIRST NAME' => $studentfirst,
                'LAST NAME' => $studentlast
            ]
        ];
    }

    print_r(json_encode($scheduleJSON, JSON_PRETTY_PRINT));

这是一个不错的代码块,虽然我不确定它是否可以重构但是,它的工作速度非常快,适合我的情况 - 这是为课程安排创建一个分组列表。

感谢大家的帮助!我肯定已经从中成长了!

此致 迈克尔Z