douou2026
2018-09-26 14:54
浏览 130
已采纳

JSON foreach循环将数据写入文件两次

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"
            },
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongsuishou8039 2018-09-26 15:21
    已采纳

    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);
      }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题