Loop through json array and insert into mysql php -


i trying insert record mysql using php don't know how achieve in php: have following json array:

[{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 below:

id | student | day

1 | 236 | 2

2 | 236 | 3

3 | 236 | 4

4 | 237 | 3

5 | 237 | 4

i using following code loop through , values out not showing each day. should create array of days student attending , loop through? please , advise appreciated. thank you.

$arr = json_decode($output,true); foreach ($arr $key => $jsons)     {         $day=null;         $student=null;          foreach($jsons $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 mytable (student,day) values($student,$day);";         echo '<br/>';     } 

you have array of arrays of key/value pairs, according code have 2 arrays of key/value pairs. see how following affects code, , throw in print statement or 2 debug.

change: foreach ($arr $key => $jsons)

to: foreach ($arr $jsons)


Comments