title: How to loop through a JSON array and put data in variables
body: <p>I am trying to get the data from an API and save it to a MySQL database. The problem is when I want to echo the data to check if it gets all the values I'm getting this error: </p>
  
  <blockquote>
    <p>Notice: Trying to get property of non-object</p>
  </blockquote>
  
  <p>My JSON data looks like this:</p>
  
  <pre><code>{"AttractionInfo":[{"Id":"sprookjesbos","Type":"Attraction","MapLocation":"1","State":"open","StateColor":"green","WaitingTime":0,"StatePercentage":0},{"Id":"zanggelukkig","Type":"Show","MapLocation":".","State":"gesloten","StateColor":"clear"},
  </code></pre>
  
  <p>I think this is because it is in an array. Because when I use this bit of code it gives no errors but I have to define the index of the array. What is the best way to loop through my data and put it in variables?</p>
  
  <pre><code>&lt;?php
  
      //Get content
      $url = "https://eftelingapi.herokuapp.com/attractions";
      $data = json_decode(file_get_contents($url));
  
      //Fetch data
      $attractieId = $data-&gt;AttractionInfo[0]-&gt;Id;
      $attractieType = $data-&gt;AttractionInfo[0]-&gt;Type;
      $attractieStatus = $data-&gt;AttractionInfo[0]-&gt;State;
      $attractieStatusKleur = $data-&gt;AttractionInfo[0]-&gt;StateColor;
      $attractieWachttijd = $data-&gt;AttractionInfo[0]-&gt;WaitingTime;
      $attractieStatusPercentage = $data-&gt;AttractionInfo[0]-&gt;StatePercentage;
  
  ?&gt;
  </code></pre>
  
  <p>I tried to find some solutions but all they use is a foreach loop. But i don't think that'll work right. Can anyone help me in the good direction or tell me how I might possibly fix this? I am not very experienced so any advice is welcome. Thanks in advance.</p>
  
  <p><strong>Update code:</strong></p>
  
  <pre><code>require 'database-connection.php';
  //Get content
  $url = "https://eftelingapi.herokuapp.com/attractions";
  $data = json_decode(file_get_contents($url));
  
  //Fetch data
  $attractieId = isset($data-&gt;AttractionInfo-&gt;Id);
  $attractieType = isset($data-&gt;AttractionInfo-&gt;Type);
  $attractieStatus = isset($data-&gt;AttractionInfo-&gt;State);
  $attractieStatusKleur = isset($data-&gt;AttractionInfo-&gt;StateColor);
  $attractieWachttijd = isset($data-&gt;AttractionInfo-&gt;WaitingTime);
  $attractieStatusPercentage = isset($data-&gt;AttractionInfo-&gt;StatePercentage);
  
  $sql =  "INSERT INTO attracties (attractieId, attractieType, attractieStatus, attractieStatusKleur, attractieWachttijd, attractieStatusPercentage) 
  VALUES ('$attractieId', '$attractieType', '$attractieStatus', '$attractieStatusKleur', '$attractieWachttijd', '$attractieStatusPercentage')";
  if ($db-&gt;query($sql) === TRUE) {
      echo "success";
  } else {
      echo "Error: " . $sql . "&lt;br&gt;" . $db-&gt;error;
  }
  </code></pre>
  
  <p>It says 'success' but when I look into my database it inserted only empty data. I need to add all the attractions to my database so not just one row. So I need to loop through my data.    </p>