I have a json file with 20 arrays(some with arrays inside arrays), I want to insert 3 different arrays into the same row on my table. Since doing each query separate would insert each array into a different row I tried to use nested for loops. The results for my code below as is were 1) only the second for loop values are inserted and the first for loop values are NULL 2) if i added a query at the end of the first for loop I get two rows,the first row with the values from the first for loop inserted and the second row contains the values from my second loop inserted. The values itself dont matter necessarily but my question is how can I insert different arrays of a JSON file into a single row on an sql table.
<?php
error_reporting(E_ALL);
$dir = "--------------------";
$connect = mysqli_connect("-----", "----", "","------");
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
foreach(glob("*.json") as $filename) {
$data = file_get_contents($filename);
$testing = json_decode($data, true);
foreach ($testing[0][0] as $Comments) {
$sql = "INSERT INTO servers_updated (Model) VALUES ('".$Comments["Model"]."')";
foreach($testing[3][1] as $row2) {
$sql = "INSERT INTO servers_updated (TotalCores)
VALUES ('".$row2['/redfish/v1/Systems/1/Processors/1/']['TotalCores']."')";
if ($connect->query($sql) === TRUE) {
echo "Data entered";
} else {
echo "Error entering data: " . $connect->error;
}
}
}
}
$connect->close();
}
}
?>