如何遍历JSON数组并将数据放入变量中

时间:2017-12-06 15:25:53

标签: php mysql arrays json

我正在尝试从API获取数据并将其保存到MySQL数据库。问题是当我想回显数据以检查它是否得到所有值我得到这个错误:

  

注意:尝试获取非对象的属性

我的JSON数据如下所示:

{"AttractionInfo":[{"Id":"sprookjesbos","Type":"Attraction","MapLocation":"1","State":"open","StateColor":"green","WaitingTime":0,"StatePercentage":0},{"Id":"zanggelukkig","Type":"Show","MapLocation":".","State":"gesloten","StateColor":"clear"},

我认为这是因为它在一个数组中。因为当我使用这段代码时它没有错误,但我必须定义数组的索引。循环我的数据并将其放入变量的最佳方法是什么?

<?php

    //Get content
    $url = "https://eftelingapi.herokuapp.com/attractions";
    $data = json_decode(file_get_contents($url));

    //Fetch data
    $attractieId = $data->AttractionInfo[0]->Id;
    $attractieType = $data->AttractionInfo[0]->Type;
    $attractieStatus = $data->AttractionInfo[0]->State;
    $attractieStatusKleur = $data->AttractionInfo[0]->StateColor;
    $attractieWachttijd = $data->AttractionInfo[0]->WaitingTime;
    $attractieStatusPercentage = $data->AttractionInfo[0]->StatePercentage;

?>

我试图找到一些解决方案,但他们使用的只是一个foreach循环。但我认为这不会正常工作。任何人都可以帮助我做好方向或告诉我如何解决这个问题?我不是很有经验,所以欢迎任何建议。提前谢谢。

更新代码:

require 'database-connection.php';
//Get content
$url = "https://eftelingapi.herokuapp.com/attractions";
$data = json_decode(file_get_contents($url));

//Fetch data
$attractieId = isset($data->AttractionInfo->Id);
$attractieType = isset($data->AttractionInfo->Type);
$attractieStatus = isset($data->AttractionInfo->State);
$attractieStatusKleur = isset($data->AttractionInfo->StateColor);
$attractieWachttijd = isset($data->AttractionInfo->WaitingTime);
$attractieStatusPercentage = isset($data->AttractionInfo->StatePercentage);

$sql =  "INSERT INTO attracties (attractieId, attractieType, attractieStatus, attractieStatusKleur, attractieWachttijd, attractieStatusPercentage) 
VALUES ('$attractieId', '$attractieType', '$attractieStatus', '$attractieStatusKleur', '$attractieWachttijd', '$attractieStatusPercentage')";
if ($db->query($sql) === TRUE) {
    echo "success";
} else {
    echo "Error: " . $sql . "<br>" . $db->error;
}

它说'成功'但是当我查看我的数据库时,它只插入了空数据。我需要将所有的吸引力添加到我的数据库,所以不只是一行。所以我需要遍历我的数据。

2 个答案:

答案 0 :(得分:2)

试试这个......

$content = json_decode(file_get_contents($url));

foreach($content->AttractionInfo as $data ){
      $id      = $data->Id;
      $type    = $data->Type;
      $map     = $data->MapLocation;
      $state   = $data->State;
      $color   = $data->StateColor;
      if(!empty($data->WaitingTime)) {
      $time = $data->WaitingTime;
      }
     if(!empty($data->StatePercentage)) {
       $percent = $data->StatePercentage;
     }

  //persist your data into DB....
}

答案 1 :(得分:0)

我认为你需要这样的东西:

    $json = '{"AttractionInfo":[{"Id":"sprookjesbos","Type":"Attraction","MapLocation":"1","State":"open","StateColor":"green","WaitingTime":0,"StatePercentage":0},{"Id":"zanggelukkig","Type":"Show","MapLocation":".","State":"gesloten","StateColor":"clear"}]}';
    $arr = json_decode($json);

     foreach ($arr->AttractionInfo as $key => $attraction) {
       foreach($attraction as $key=> $value) {
          print_r($key.' - '.$value.'<br>');
          $$key = $value;
       }
    }

echo '<br>';
echo $Id; // it will be last item/attraction id.

我们可以改进此代码。只是说你想在哪里以及如何使用它