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



这是我第一次使用 json,只是玩弄一些数据。

这是我的json的一部分

Array
(
[DataSet] => Array
(
[Table] => Array
(
[0] => Array
(
[Driver] => John Doe
[Shift Date] => 2018-01-05T00:00:00-05:00

我希望能够获取所有 [驱动程序] 和 [轮班日期] 字段并将它们放入表中。它有 2 列驱动程序和 ShiftDate,不确定我从这里开始。

经过大量的谷歌搜索,我想出了解决方案,如果我没有正确表达我的问题,我深表歉意,这是我第一次破解这个问题,但我认为发布答案可能会帮助其他人试图解决这个问题:

$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();  
}

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

$dates = array_column("轮班日期",$yourData(;

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

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

最新更新