使用PHP将Json数组嵌套到Mysql中

时间:2018-10-24 20:18:54

标签: php mysql json

这是我第一次使用json,只是处理一些数据。

这是我的json的一部分

if("B" + x.ToString()+ y.ToString() == "B00")
{
    B00.IsEnabled = false; //B00 is the button name f.e.
}

我希望能够获取所有[Driver]和[Shift Date]字段并将它们放入表格中。它有2列Driver和ShiftDate,不确定我从哪里去。

2 个答案:

答案 0 :(得分:0)

经过大量的搜索后,我想出了解决方案,如果无法正确回答我的问题,我深表歉意,这是我对此的初衷,但我认为发布答案可能会帮助其他人解决这个问题:

$str = file_get_contents('./cgapi.json');
$json = json_decode($str, true);
foreach($json['DataSet']['Table'] as $item) {
    $stmt = $dbcon->prepare("INSERT INTO DriverData (DriverName, ShiftDate, Minutes, Stops, Gallons) VALUES (?,?,?,?,?)");
    $stmt->bind_param("sssss", $item['Driver'], $item['Shift Date'], $item['Minutes'], $item['Stops'], $item['Gallons']);
    $stmt->execute();
    $stmt->close();  
}

答案 1 :(得分:-1)

$ drivers = array_column('Driver',$ yourData);

$ dates = array_column('Shift Date',$ yourData);

Array列提取作为第一个参数提供的键的所有值。

http://php.net/manual/en/function.array-column.php