0
$cn = curl_init();
$url = 'https://URL';
curl_setopt($cn, CURLOPT_URL, $url);
curl_setopt($cn, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($cn);
$aOutput = json_decode($output, TRUE);
//var_dump($aOutput);
$curl_error = curl_error($cn);
print_r($curl_error);
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach($aOutput as $key => $data) {
var_dump($key['average_price']);
// var_dump($data[$key]["Average_Price"]);
// $sql = "INSERT INTO Market('type_id', 'average_price', 'adjusted_price')
// VALUES ($data);";
}
array(10763) {
[0]=>
array(3) {
["average_price"]=>
float(381907.23)
["adjusted_price"]=>
float(383184.18)
["type_id"]=>
int(32772)
}
[1]=>
array(3) {
["average_price"]=>
float(54090.07)
["adjusted_price"]=>
float(57340.16)
["type_id"]=>
int(32774)
}
我正試圖將這整個多維數組添加到我的數據庫。我遇到的主要問題是foreach循環。將RestAPI響應添加到MySQL數據庫
我試過$ array [$ key] [$ column],還有很多其他的方法,但是我在轉儲這個變量的時候只有NULL。
我試過,$ key ['average_price']等仍然返回null。
在你的foreach中, '$ key ['average_price']'等等應該可以正常工作 – WillardSolutions
'$ aOutput'的結構是什麼?我建議你需要從'$ data ['average_price']'而不是'$ key'獲取數據,我猜這是標量。你的循環將'$ aOutput'的每一項都表示爲'$ key'和'$ data',其中'$ key'是一個數組索引(在這種情況下),'$ data'是一個哈希本身。 – Wizard