问题描述:

I want to know how to add new data in json file array

i am new to json.

want to add data from php or html input.

[

{

"id":1,

"name":"abc",

"value":"abcd"

},

{

"id":2,

"name":"Kiss",

"value":"Kiss"

}

{

"id":3,

"name":"efgh",

"value":"efgh"

}

]

How can i add forth item of array in json file with php or html input

please help me with example ,

thanks

网友答案:

Firstly, I'll just note the JSON you showed isn't valid (it's missing a comma :P), but here is the answer:

You have to load it in.

$data = json_decode($json, true);

($json should be the JSON, and true is to specify to decode as associative array where necessary)

then add your element,

$data[] = array('id'=>4, 'name'=>'abcdef', 'value'=>'abcdefg');

and then you can recode it as JSON

$json = json_encode($data);
相关阅读:
Top