简体   繁体   English

循环遍历json数组并插入mysql php

[英]Loop through json array and insert into mysql php

I am trying to insert a record into mysql using php but I don't know how to achieve this in php: Basically I have the following json array: 我正在尝试使用php将记录插入mysql,但我不知道如何在php中实现:基本上我有以下json数组:

[{StudentId:236,"Monday":null,"Tuesday":"2","Wednesday":"3","Thursday":"4","Friday":null},{StudentId:237,"Monday":null,"Tuesday":null,"Wednesday":"3","Thursday":"4","Friday":"5"}]

MySQL table schema is below: MySQL表架构如下:

Id | ID | Student | 学生| Day

1 | 1 | 236 | 236 | 2 2

2 | 2 | 236 | 236 | 3 3

3 | 3 | 236 | 236 | 4 4

4 | 4 | 237 | 237 | 3 3

5 | 5 | 237 | 237 | 4 4

I am using the following code to loop through and get the values out but it is not showing for each day. 我正在使用以下代码循环遍历并获取值,但并非每天都在显示。 Should I create another array of all the days the student is attending and then loop through? 我是否应该在学生上课的所有日子中创建另一个数组,然后进行遍历? Please and advise would be much appreciated. 请和建议将不胜感激。 Thank you. 谢谢。

$arr = json_decode($output,true);
foreach ($arr as $key => $jsons)
    {
        $day=null;
        $student=null;

        foreach($jsons as $key => $value)
        {
            if($key == 'StudentId')
            {
                $student = $value;
            }

            if($key == 'Monday')
            {
              if($value == '1')
              {
                 $day = 1;
              }
            }

            if($key == 'Tuesday')
            {
               if($value == '2')
               {
                   $day = 2;
               }
             }
        }
        echo "INSERT INTO MyTable (Student,Day) VALUES($student,$day);";
        echo '<br/>';
    }

You have an array of arrays of key/value pairs, but according to your code you have two arrays of key/value pairs. 您有一组键/值对数组,但是根据您的代码,您有两个键/值对数组。 See how the following affects your code, and throw in a print statement or two to help debug. 查看以下内容如何影响您的代码,并抛出一两个print语句以帮助调试。

Change: foreach ($arr as $key => $jsons) 更改: foreach ($arr as $key => $jsons)

to: foreach ($arr as $jsons) 到: foreach ($arr as $jsons)

it looks like You have malformed JSON string. 看起来您的JSON字符串格式错误。 This sample works fine: 此示例工作正常:

$json = <<<JSON
[
  {
    "StudentId": 236,
    "Monday": null,
    "Tuesday": "2",
    "Wednesday": "3",
    "Thursday": "4",
    "Friday": null
  },
  {
    "StudentId": 237,
    "Monday": null,
    "Tuesday": null,
    "Wednesday": "3",
    "Thursday": "4",
    "Friday": "5"
  }
]
JSON;

$arr = json_decode($json, true);
foreach ($arr as $student) {
    foreach ($student as $key => $value) {
        if ($key == 'StudentId') {
            $student = $value;
        }

        if ($key == 'Monday') {
            if ($value == '1') {
                $day = 1;
            }
        }

        if ($key == 'Tuesday') {
            if ($value == '2') {
                $day = 2;
            }
        }
    }
    echo "INSERT INTO MyTable (Student,Day) VALUES($student,$day);\n";
    echo '<br/>';
}

And the result is: 结果是:

INSERT INTO MyTable (Student,Day) VALUES(236,2);
INSERT INTO MyTable (Student,Day) VALUES(237,2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM