簡體   English   中英

PHP 循環遍歷 JSON 響應並插入 MySQL

[英]PHP loop through JSON response and insert into MySQL

對於學校項目,我想遍歷 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/>';

}
?>

編輯:當前代碼:

$pdo = new PDO('mysql:host=localhost;dbname=XXX', 'XXX', 'XXX');
$yourJsonArray ="test.json";
$dataArray = json_decode(json_encode($yourJsonArray),true);
foreach($dataArray as $key => $value){
   $image = $key['image'];
   $statement = $pdo->prepare('INSERT INTO api_data (image) VALUES (?)'); 
   $statement->execute(array($image));
}

錯誤消息:警告:為第 29 行 test.php 中的 foreach() 提供的參數無效

第 29 行 = foreach(上面有一些舊評論)

編輯:添加var_dump($yourJsonArray); 在 foreach 之前:

string(638) "{"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"}"
Warning: Invalid argument supplied for foreach() in test.php on line 30

編輯:將 $dataArray 更改為$dataArray = json_decode($yourJsonArray,true);

錯誤 (4x):警告:第 33 行 test.php 中的非法字符串偏移“圖像”

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

編輯:我丟棄了 $key Var 並得到了這個:

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM