【问题标题】:How to add data in JSON using PHP如何使用 PHP 在 JSON 中添加数据
【发布时间】:2019-01-28 01:02:46
【问题描述】:

我有一个 json 文件,语法如下:

[
  {
    "fields": {
      "service_number": "service_number",
      "physical_address": "physical_address",
      "account_id": "account_id",
      "contact_id": "contact_id"
    },
    "someId": "asd23f",
    "status": "Active",
    "perCode": "1",
    "idCode": "0987",
    "nextCode": "09"
  },
  {
    "fields": {
      "service_number": "service_number",
      "physical_address": "physical_address",
      "account_id": "account_id",
      "contact_id": "contact_id"
    },
    "someId": "789096",
    "status": "Active",
    "perCode": "1",
    "idCode": "076543",
    "nextCode": "09"
  }
]

我想使用 for 循环在 nextCode 之前或之后添加类似 userID 的内容。有什么解决办法吗? 到目前为止,我试过这个:

$data = json_decode($file, true);
foreach ($data as $key => $val)
{
    foreach ($val as $key=>$c)
    {
        if (is_array($c))
            continue;
        $data .= $data . "user_id:$userId";
    }
}

当然不会成功,请问有什么想法吗?

【问题讨论】:

    标签: php json for-loop


    【解决方案1】:

    有一些问题。

    • 首先,foreach 循环使用它正在迭代的数组的副本,因此修改其中的一项不会改变原始数组。

    • 然后,您的内部 foreach 循环将覆盖外部循环中的 $key。这引起问题,但没关系,因为您实际上并不需要内部循环。

    • 1234563在它的末尾而不是在你的循环所在的特定点。

    只需参考您需要的特定键并在那里设置值。

    $data = json_decode($file, true);
    foreach ($data as $key => $val)
    {
        $data[$key]['user_id'] = $userId;
    }
    $data = json_encode($data);
    

    【讨论】:

    • 哇!感谢大家的宝贵贡献!我发现劳伦斯和拉斐尔的答案都是正确的,我更喜欢使用数组。所有最好的人!
    • 不客气!如果您最终使用带有引用的 foreach 循环,请确保在循环之后取消设置引用,否则可能会发生一些奇怪的事情。 (您可以查看 foreach 文档以获取更多具体信息)
    【解决方案2】:

    另一种稍微短一点的方法是通过引用&传递值:

    $data = json_decode($file, true);
    foreach ($data as &$val) {
        $val['user_id'] = $userId;
    }
    

    https://3v4l.org/ZF4Ve

    【讨论】:

      【解决方案3】:

      这是您可以将元素添加到已解析的 JSON 并将其重新构建回 JSON 的方式:

      // parse the JSON into an array
      $data = json_decode($file, true);
      
      foreach ($data as $key => $val)
      {
          // add the userID to each element of the main array
          // it will be inserted after the last element (after nextCode)
          $data[$key]['userID'] = 'some-id';
      }
      
      // if needed, parse the array back to JSON
      $data = json_encode($data);
      

      【讨论】:

        【解决方案4】:

        我给你解释

        下面的表达式将信息从字符串转换为数组

        $data = json_decode ($file, true);
        

        所以在你的 foreach 中你只需要添加键

        $data = json_decode($file, true);
        foreach ($data as $key => $val)
        {
            foreach ($val as $k=>$c)
            {
                if (is_array($c))
                    continue;
                $data[$k]['user_id'] = $userId;
            }
        }
        

        【讨论】:

          【解决方案5】:

          我看到每个人都回答了数组选项。不过,我不明白为什么不使用对象。

          我会这样做:

          $data = json_decode($file);
          foreach ($data as &$field)
          {
              $field->userID = $userId;
          }
          
          $data = json_encode($data);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-06-21
            • 2013-03-24
            相关资源
            最近更新 更多