0

I have the following json data

{
  "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"
    }
  ]
}

I'm attempting to convert this to an array in PHP using json_decode, remove an item from the array based on the array position / index, then convert the array back to JSON using json_encode.

To modify the array, im using the following PHP command

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

Where $i is the index position of the item being removed.

it's all working almost perfectly except that in the final (output) JSON data, I noticed I get key values added to each "apps" object (note the 'number key before each object')

{
  "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"
    }        
  }
}

How can I avoid this being added when I perform json_encode or after the fact, remove it so that the data is structured exactly like the original version?

tamak
  • 1,541
  • 2
  • 19
  • 39
  • Could you show the code you use to modify the array? – TecBrat Oct 06 '17 at 14:42
  • You likely need to rebase the array before converting back to JSON, see this related question: https://stackoverflow.com/questions/5943149/rebase-array-keys-after-unsetting-elements – Rowan Gray Oct 06 '17 at 14:44
  • @TecBrat its been marked as duplicate but I edited and added details on what I'm doing to the array – tamak Oct 06 '17 at 14:47

0 Answers0