title: JSON foreach loop writing data twice to file
body: <p>I am trying to process the JSON response differently based on whether there is just 1 row in the result or more than 1 row.</p>
  
  <p>However, the data being pulled from the result is being written to the file twice.</p>
  
  <p>Can someone see why?</p>
  
  <p>** Added **</p>
  
  <pre><code>$module = $_POST['module'];
  // Get The Data
  $json = file_get_contents($url);
  $obj = json_decode($json);
  $row = $obj-&gt;response-&gt;result-&gt;$module-&gt;row;
  // Count Rows &amp; Fields
  $countRows = count($row);
  $fields = $obj-&gt;response-&gt;result-&gt;$module-&gt;row;
  if($countRows == 1)
  {
      $row = $obj-&gt;response-&gt;result-&gt;$module-&gt;row;
      $countFields = count($row-&gt;FL);
      foreach($row as $r) 
      {
           $i = 0;
           foreach($row-&gt;FL as $data)
           {
               $i++;
               if($i != $countFields)
               {
                   $csvfile = $module.'.csv';
                   $file = fopen($csvfile, "a");
                   $write = $data-&gt;content.',';
                   fwrite($file, $write);
                   fclose($file);
              } 
              else 
              {
                  $file = fopen($csvfile, "a");
                  $write = $data-&gt;content.PHP_EOL;
                  fwrite($file, $write);
                  fclose($file);
              }
          }
      } 
  } else {
          $row = $obj-&gt;response-&gt;result-&gt;$module-&gt;row;
          $countFields = count($row[0]-&gt;FL);
          foreach($row as $r) 
          {
              $i = 0;
              foreach($r-&gt;FL as $data)
              {
                  $i++;
                  if($i != $countFields)
                  {
                  $csvfile = $module.'.csv';
                  $file = fopen($csvfile, "a");
                  $write = $data-&gt;content.',';
                  fwrite($file, $write);
                  fclose($file);
                  } else {
                  $file = fopen($csvfile, "a");
                  $write = $data-&gt;content.PHP_EOL;
                  fwrite($file, $write);
                  fclose($file);
                  }
              }
          }
      } 
  }
  </code></pre>
  
  <p>And what's puzzling me even more is that I'm getting an error:</p>
  
  <blockquote>
    <p>Undefined property: stdClass::$content</p>
  </blockquote>
  
  <p>here</p>
  
  <pre><code> $write = $data-&gt;content.',';
  </code></pre>
  
  <p>Yet it's writing the data to file <em>twice</em> for some unknown reason.</p>
  
  <p><strong>The error above only occurs on JSON results where there is only 1 record being returned.</strong></p>
  
  <p>Sample JSON result</p>
  
  <pre><code>{
    "response": {
      "result": {
        "Deals": {
          "row": {
            "no": "1",
            "FL": [
              {
                "val": "DEALID",
                "content": "3508588000000206039"
              },
  </code></pre>