Chris Yates
Chris Yates

Reputation: 243

JSON foreach loop writing data twice to file

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.

However, the data being pulled from the result is being written to the file twice.

Can someone see why?

** Added **

$module = $_POST['module'];
// Get The Data
$json = file_get_contents($url);
$obj = json_decode($json);
$row = $obj->response->result->$module->row;
// Count Rows & Fields
$countRows = count($row);
$fields = $obj->response->result->$module->row;
if($countRows == 1)
{
    $row = $obj->response->result->$module->row;
    $countFields = count($row->FL);
    foreach($row as $r) 
    {
         $i = 0;
         foreach($row->FL as $data)
         {
             $i++;
             if($i != $countFields)
             {
                 $csvfile = $module.'.csv';
                 $file = fopen($csvfile, "a");
                 $write = $data->content.',';
                 fwrite($file, $write);
                 fclose($file);
            } 
            else 
            {
                $file = fopen($csvfile, "a");
                $write = $data->content.PHP_EOL;
                fwrite($file, $write);
                fclose($file);
            }
        }
    } 
} else {
        $row = $obj->response->result->$module->row;
        $countFields = count($row[0]->FL);
        foreach($row as $r) 
        {
            $i = 0;
            foreach($r->FL as $data)
            {
                $i++;
                if($i != $countFields)
                {
                $csvfile = $module.'.csv';
                $file = fopen($csvfile, "a");
                $write = $data->content.',';
                fwrite($file, $write);
                fclose($file);
                } else {
                $file = fopen($csvfile, "a");
                $write = $data->content.PHP_EOL;
                fwrite($file, $write);
                fclose($file);
                }
            }
        }
    } 
}

And what's puzzling me even more is that I'm getting an error:

Undefined property: stdClass::$content

here

 $write = $data->content.',';

Yet it's writing the data to file twice for some unknown reason.

The error above only occurs on JSON results where there is only 1 record being returned.

Sample JSON result

{
  "response": {
    "result": {
      "Deals": {
        "row": {
          "no": "1",
          "FL": [
            {
              "val": "DEALID",
              "content": "3508588000000206039"
            },

Upvotes: 0

Views: 90

Answers (1)

dWinder
dWinder

Reputation: 11642

Notice that $row is object and not and array (as can be seen in your example).

It has 2 field: "no" and "FL". So when you loop with foreach($row as $r) you will have 2 round (looping on json object).

So now you doing the inner scope twice. Which have:

foreach($row->FL as $data) // here referring to base $row
 ...
    $write = $data->content.',';

I suggest keep the if ($countRows == 1) option like this:

if($countRows == 1)
{
  $row = $obj->response->result->$module->row;
  $csvfile = $module.'.csv';
  $countFields = count($row->FL);
  $i = 0;
  foreach($row->FL as $data)
  {
      $i++;
      $file = fopen($csvfile, "a");
      $write = $data->content;
      if($i != $countFields)
          $write .= ',';
      else 
          $write .= PHP_EOL;
      fwrite($file, $write);
      fclose($file);
  }
}

Upvotes: 1

Related Questions