Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP通过JSON响应循环并插入MySQL_Php_Mysql_Json_Loops_Foreach - Fatal编程技术网

PHP通过JSON响应循环并插入MySQL

PHP通过JSON响应循环并插入MySQL,php,mysql,json,loops,foreach,Php,Mysql,Json,Loops,Foreach,对于一个学校项目,我想循环一个JSON响应并将数据插入MySQL数据库 JSON响应如下所示: {"tableID":100965,"data":[{"updated_at":1506994152000,"prices":{"unstable_reason":"LOW_SALES","unstable":true,"sold":{"avg_daily_volume":null,"last_90d":67,"last_30d":52,"last_7d":15,"last_24h":1},"max"

对于一个学校项目,我想循环一个JSON响应并将数据插入MySQL数据库

JSON响应如下所示:

{"tableID":100965,"data":[{"updated_at":1506994152000,"prices":{"unstable_reason":"LOW_SALES","unstable":true,"sold":{"avg_daily_volume":null,"last_90d":67,"last_30d":52,"last_7d":15,"last_24h":1},"max":287.5,"avg":93.52,"min":51.01,"latest":87},"image":"IMGURL","db_name":"Test-Artikel","dbID":"123456789"},{"updated_at":1506994152000,"prices":{"unstable_reason":"LOW_SALES","unstable":true,"sold":{"avg_daily_volume":null,"last_90d":67,"last_30d":52,"last_7d":15,"last_24h":1},"max":287.5,"avg":93.52,"min":51.01,"latest":87},"image":"IMGURL","db_name":"Test-Artikel","dbID":"123456789"}],"__v":0,"createdAt":"2017-03-27T09:16:48.395Z"}
每个数组都应该是mysql数据库中自己的一行

目前,我可以通过以下代码从json文件中获取一个信息:

<?php
$url = "linktojson";

//connect to database
//$pdo = new PDO('mysql:host=localhost;dbname=...', '...', '...');

//read the json file contents
$jsondata = file_get_contents($url);

//convert json object to php associative array
$data = json_decode($jsondata, true);   

echo('<pre>');
foreach ($data['data'] as $api_data) {
    echo $api_data['name'] . '<br/>';

}
?>
错误消息:警告:第29行test.php中为foreach()提供的参数无效

第29行=foreach(顶部有一些旧注释)

编辑:添加了
var\u转储($yourJsonArray)foreach之前:

string(7) "tableID" string(4) "data" string(3) "__v" string(9) "createdAt"
编辑:将$dataArray更改为
$dataArray=json\u decode($yourJsonArray,true)

错误(4x): 警告:第33行test.php中的非法字符串偏移量“image”

第33行=
$image=$key['image']

编辑:我转储了$key变量,得到了以下结果:

$yourJsonArray = "You Json Array will come here";
//$dataArray = json_decode(json_encode($yourJsonArray),true);
$dataArray = json_decode($yourJsonArray,true); 
foreach($dataArray as $key => $value){
   //Here 0,1,2,3 Will be contained inside the $key variable.
   //Code to insert the data comes here
}
如何获取“数据”元素?

请尝试以下操作:


我知道如何循环文件和每个数组中的一个元素(例如“name”)。回声(“”);foreach($data['data']as$name){echo$name['name'].
;}I'无法找到正确的方法来获取所需的所有元素。您的“json响应”看起来像这样,但这不是json。把真实的东西放在人们可以帮助你的地方,而不是你认为你得到了什么,或者你认为你在“linktojson”制作了什么。我明白你的意思-将json文件的前两个元素添加到我的question.ahhh。。。他的眼睛看到了(jsonlint认为这个json是有效的)!因此,
$api_data
数组似乎没有
名称
属性。你是说
db\u name
?哈哈。。我的问题中的php代码是“过时的”-我现在使用的是db_名称。谢谢-我会尝试一下!我试过了,但出现了一个错误(见上面的问题)@FelixSchwie在将其发送到
foreach()
?@FelixSchwie我已经编辑了答案,删除
var\u dump()
。在问题中添加了新的ErrorMSG
string(7) "tableID" string(4) "data" string(3) "__v" string(9) "createdAt"
$yourJsonArray = "You Json Array will come here";
//$dataArray = json_decode(json_encode($yourJsonArray),true);
$dataArray = json_decode($yourJsonArray,true); 
foreach($dataArray as $key => $value){
   //Here 0,1,2,3 Will be contained inside the $key variable.
   //Code to insert the data comes here
}