-2

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

3

1 Answer 1

10

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);
Sign up to request clarification or add additional context in comments.

1 Comment

can you please help me ? can you give me example with php file , i actually dont know how to add with php also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.