Guesser
Guesser

Reputation: 1857

PHP adding to array after JSON encoding

I'm encoding a very basic array like this.

$c['A']=NULL;

$c=json_encode($c);

I later want to decode the json and add a new key

$c=json_decode($c);

$c[B]=NULL;

The problem is that $c is now an object.

Upvotes: 0

Views: 45

Answers (2)

egig
egig

Reputation: 4430

json_decode() is returning PHP Object. You need 2nd parameter if you want it to be array:

$c=json_decode($c, true);

look:http://php.net/manual/en/function.json-decode.php

When TRUE, returned objects will be converted into associative arrays.

Upvotes: 2

user1823761
user1823761

Reputation:

Use $c as an object:

$c->B = NULL;

Upvotes: 0

Related Questions