【问题标题】:How to prevent overriding in array?如何防止在数组中覆盖?
【发布时间】:2018-06-14 10:00:54
【问题描述】:

我正在尝试将文件中可用的不同 json 的所有键添加到数组中。我目前所做的是:

  //Get the json file content
  $jsonData = file(__DIR__ .'/../logs/error.json');

  //Save all the json
  $json = [];

  //Iterate through the line of the file, each line is a json
  foreach($jsonData as $line)
  {
    //Convert the json in an associative array
    $array = json_decode($line, true);

    //Iterate through the json keys
    foreach($array as $k => $val)
    {
      $json[$k] = $val;
    }
  }

json文件是这样的:

{"Timestamp":"2018-06-14T10:46:52.3326036+02:00","Level":"Error","MessageTemplate":"System.Exception"}
{"Timestamp":"2018-06-14T10:47:22.7493871+02:00","Level":"Error","MessageTemplate":"System.Exception"}

我会得到这个:

{"Timestamp":"2018-06-14T10:47:22.7493871+02:00","Level":"Error","MessageTemplate":"System.Exception"}

因为$json[$k]覆盖了我猜是之前的数组,但是$k是新的json那么为什么要替换数组的索引呢?

提前感谢您的帮助。

【问题讨论】:

    标签: php json


    【解决方案1】:

    这可能是您的预期输出。

    //Get the json file content
      $jsonData = file(__DIR__ .'/../logs/error.json');
    
      //Save all the json
      $json = [];
    
      //Iterate through the line of the file, each line is a json
      foreach($jsonData as $line)
      {
        //Convert the json in an associative array
        $array = json_decode($line, true);
    
        $temp = [];
        //Iterate through the json keys
        foreach($array as $k => $val)
        {
          $temp[$k] = $val;
        }
        $json[] = $temp;
      }
    

    【讨论】:

      【解决方案2】:

      改变这一行

      foreach($array as $k => $val)
          {
            $json[$k] = $val;
          }
      

      foreach($array as $k => $val)
          {
            $json[][$k] = $val;
          }
      

      【讨论】:

        【解决方案3】:

        嗯,你用相同的名字覆盖了键,所以你的输出真的没有什么令人惊讶的。

        你可能打算这样做:

        foreach($jsonData as $line) {
            $tmp = []; //<-- set up a new array just for this iteration
            $array = json_decode($line, true);
            foreach($array as $k => $val) $tmp[$k] = $val;
            $json[] = $tmp; //<-- log the array in the master $json array
        }
        

        【讨论】:

          【解决方案4】:
          //Get the json file content
          
            $jsonData = file(__DIR__ .'/../logs/error.json');
            
          //Convert the json in an associative array
          
            $array = json_decode($jsonData, true);
            
          //Save all the json
          
            $json = [];
          
            //Iterate through the line of the file, each line is a json
          
            foreach($array as $k => $val)
            {
          
              $json[][$k] = $val;
              
            }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-19
            • 2021-02-08
            • 2014-05-19
            • 2020-07-23
            • 1970-01-01
            相关资源
            最近更新 更多