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

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);
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像