如何避免json_encode向我的数组对象添加键值?

时间:2017-10-06 14:40:10

标签: php arrays json

我有以下json数据

{
  "apps": [
    {
      "thingA": "aaaaaa",
      "thingB": "bbbbbb",
      "thingC": "cccccc",
      "thingD": {
        "thingD1": "d1d1d1d1",
        "thingD2": "d2d2d2d2"
      },
      "thingE": "eeeeee",
      "out_file": "ffffff"
    },
{
      "thingA": "aaaaaa",
      "thingB": "bbbbbb",
      "thingC": "cccccc",
      "thingD": {
        "thingD1": "d1d1d1d1",
        "thingD2": "d2d2d2d2"
      },
      "thingE": "eeeeee",
      "out_file": "ffffff"
    },
{
      "thingA": "aaaaaa",
      "thingB": "bbbbbb",
      "thingC": "cccccc",
      "thingD": {
        "thingD1": "d1d1d1d1",
        "thingD2": "d2d2d2d2"
      },
      "thingE": "eeeeee",
      "out_file": "ffffff"
    }
  ]
}

我尝试使用json_decode将其转换为PHP中的数组,根据数组位置/索引从数组中删除项目,然后使用json_encode将数组转换回JSON。

要修改数组,请使用以下PHP命令

unset($configuration_Array->{'apps'}[$i]);

其中$ i是要删除的项目的索引位置。

除了在最终(输出)JSON数据中,我注意到我将每个" apps"添加了关键值,所以它的工作几乎完全正常。对象(注意每个对象之前的数字键')

{
  "apps":{
    "1":{
      "thingA":"aaaaaa",
      "thingB":"bbbbbb",
      "thingC":"cccccc",
      "thingD":{
        "thingD1":"d1d1d1d1",
        "thingD2":"d2d2d2d2"
      },
      "thingE":"eeeeee",
      "out_file":"ffffff"
    },
    "2":{
      "thingA":"aaaaaa",
      "thingB":"bbbbbb",
      "thingC":"cccccc",
      "thingD":{
        "thingD1":"d1d1d1d1",
        "thingD2":"d2d2d2d2"
      },
      "thingE":"eeeeee",
      "out_file":"ffffff"
    },
    "3":{
      "thingA":"aaaaaa",
      "thingB":"bbbbbb",
      "thingC":"cccccc",
      "thingD":{
        "thingD1":"d1d1d1d1",
        "thingD2":"d2d2d2d2"
      },
      "thingE":"eeeeee",
      "out_file":"ffffff"
    }        
  }
}

当我执行json_encode或事后删除它以便数据结构与原始版本完全相同时,如何避免添加?

0 个答案:

没有答案