<?php
$json = "{
\"Data 1\": {
\"Text1\": \"Anything1\",
\"Text2\": \"Anything2\"
},
\"Data 2\": {
\"2018-08-02\": {
\"1.\": \"145\",
\"2.\": \"258\"
},
\"2018-08-03\": {
\"1.\": \"428\",
\"2.\": \"528\"
},
\"2018-08-04\": {
\"1.\": \"727\",
\"2.\": \"514\"
}
}
}";
$jsonDecoded = json_decode($json, true);
foreach ($jsonDecoded['Data 2'] as $dateKey => $data) {
//$values = '';
//foreach ($data as $datum) {
// $values .= $datum . ',';
//}
$values = implode(',', $data);
$statement = "INSERT INTO mytable (TheDate, Value1, Value2) VALUES ('{$dateKey}'," . $values . ");";
echo $statement . PHP_EOL;
}
- Output for 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- INSERT INTO mytable (TheDate, Value1, Value2) VALUES ('2018-08-02',145,258);
INSERT INTO mytable (TheDate, Value1, Value2) VALUES ('2018-08-03',428,528);
INSERT INTO mytable (TheDate, Value1, Value2) VALUES ('2018-08-04',727,514);
preferences:
145.6 ms | 408 KiB | 5 Q