title: Getting API response using cURL in PHP and storing the response in a MySQL Database. Cycling through the array and inserting/updating the database
body: <p>I'm currently working on a project where I have to import some statistics into an MySQL Database. I'm able to retrieve a JSON String using the cURL function of PHP. After receiving the API response I convert it using:</p>
  
  <pre><code>$decoded_json = json_decode($api_response)
  </code></pre>
  
  <p>I now have an array that looks like the following (to make it easier I cut the array midway through)</p>
  
  <pre><code>array (
  'status' =&gt; 'ok',
  'meta' =&gt; 
  array (
    'count' =&gt; 1,
  ),
  'data' =&gt; 
  array (
  502268596 =&gt; 
  array (
    0 =&gt; 
    array (
      'random' =&gt; 
      array (
        'damage_dealt' =&gt; 1443,
        'wins' =&gt; 4,
        'frags' =&gt; 5,
        'spotted' =&gt; 3,
        'battles' =&gt; 8,
      ),
      'tank_id' =&gt; 8257,
    ),
    1 =&gt; 
    array (
      'random' =&gt; 
      array (
        'damage_dealt' =&gt; 155992,
        'wins' =&gt; 60,
        'frags' =&gt; 90,
        'spotted' =&gt; 85,
        'battles' =&gt; 99,
      ),
      'tank_id' =&gt; 3921,
    ),
    2 =&gt; 
    array (
      'random' =&gt; 
      array (
        'damage_dealt' =&gt; 48407,
        'wins' =&gt; 27,
        'frags' =&gt; 58,
        'spotted' =&gt; 44,
        'battles' =&gt; 53,
      ),
      'tank_id' =&gt; 3713,
    ), 
  </code></pre>
  
  <p>From that array I need to write the field "damage_dealt","wins","frags","spotted","battles" and "tank_id" into my database. I've already set up a connection to my database using mysqli.</p>
  
  <p>Additional to that I need to store the "502268596" in the database  which is the "account_id".</p>
  
  <p>The problem I'm having now is: How do I cycle through that array and get the fields into my database while they update if "account_id" and "tank_id" are already in the database? Right now I've set "account_id" and "tank_id" as Primary Keys for the database.</p>
  
  <p>Thanks in advance</p>
  
  <p>EDIT: in case you need the API response for whatever reason:</p>
  
  <p><a href="https://developers.wargaming.net/reference/all/wot/tanks/stats/?application_id=95f97675107994aa1aa0956246152c13&amp;account_id=502268596&amp;extra=random&amp;fields=tank_id%2Crandom.battles%2Crandom.damage_dealt%2Crandom.frags%2Crandom.spotted%2Crandom.wins&amp;in_garage=1&amp;r_realm=eu&amp;run=1#" rel="nofollow noreferrer">Link</a></p>
  
  <p>I've found a way to access the fields in the meantime.</p>
  
  <pre><code>//Getting tank_id
  $tank_id = $decoded_json[data][$account_id][0][tank_id]
  //getting the rest
  $damage_dealt = $decoded_json[data][$account_id][0][random][damage_dealt]
  //etc.
  </code></pre>
  
  <p>So my question now is: How do I cycle through the "0" part of the array and how do I update the values in my database if they already exist? Do I have to query my database for tank_id and account_id and then make a second query (insert/update) based on tank_id and account_id already being in my database?</p>
  
  <p>working code to access all fields:</p>
  
  <pre><code>for ($i = 0; i&lt; count($decode[data][$_SESSION['account_id']][$i]); $i++){
      $tank_id = $decode[data][$_SESSION['account_id']][$i][tank_id];
      $damage_dealt = $decode[data][$_SESSION['account_id']][$i][random][damage_dealt];
  //etc.
  }
  </code></pre>
  
  <p>EDIT: Working Code-Sample:</p>
  
  <pre><code>for ($i = 0; i&lt; count($decode[data][$_SESSION['account_id']][$i]); $i++){
      $tank_id = $decode[data][$_SESSION['account_id']][$i][tank_id];
      $damage_dealt = $decode[data][$_SESSION['account_id']][$i][random]    [damage_dealt];
      $wins = $decode[data][$_SESSION['account_id']][$i][random][wins];
      $frags = $decode[data][$_SESSION['account_id']][$i][random][frags];
      $spotted = $decode[data][$_SESSION['account_id']][$i][random][spotted];
      $battles = $decode[data][$_SESSION['account_id']][$i][random][battles];
      $sql = "INSERT INTO usertanks (account_id, tank_id, damage_dealt, frags, spotted, wins, battles) VALUES     ('$account_id','$tank_id','$damage_dealt','$frags','$spotted','$wins','$battles')    
      ON DUPLICATE KEY UPDATE
      damage_dealt='$damage_dealt', frags='$frags', spotted = '$spotted',      wins='$wins', battles = '$battles'";
      mysqli_query($db,$sql);
  }
  </code></pre>