5

I have Json in PHP like this:

$json = '{"total":"100", "page":"1", "records":"100", "rows": [ 
{"no":"1","part_number":"2","part_name":"3","price":"4","note":"8"}]}';

and I want to add

"test1":"5","test2":"7"

into JSON above.

so it will be like this:

$json = '{"total":"100", "page":"1", "records":"100", "rows": [ 
    {"no":"1","part_number":"2","part_name":"3","price":"4","test1":"5","test2":"7","note":"8"}]}';

Please, help me. How to add attribute in JSON in PHP?

Rio Eduardo
  • 233
  • 2
  • 4
  • 8
  • You could parse the json string into an array using `json_decode`, add the new attributes to the array, and recreate the json string using `json_encode`. – FThompson Jun 19 '13 at 16:19
  • possible duplicate of [Add new data into PHP JSON string](http://stackoverflow.com/questions/1745052/add-new-data-into-php-json-string) – Felix Kling Jun 19 '13 at 16:23
  • the examples in http://stackoverflow.com/questions/1745052/add-new-data-into-php-json-string use array definitions. Rio is wanting to add attributes(properties) – user20232359723568423357842364 Jun 19 '13 at 16:30
  • @user20232359723568423357842364: You can simply pass `true` as second argument to `json_decode`. No difference. Apparently 4 years ago people didn't require so much hand-holding. – Felix Kling Jun 19 '13 at 16:35
  • Beginners have always required hand-holding ;) 4 years ago there weren't as many around here – user20232359723568423357842364 Jun 19 '13 at 16:44

1 Answers1

10
$json = '{"total":"100", "page":"1", "records":"100", "rows": [ 
{"no":"1","part_number":"2","part_name":"3","price":"4","note":"8"}]}';

// decode json
$json = json_decode($json);

// add data
$json->rows[0]->test1 = "5";
$json->rows[0]->test2 = "7";

// echo it for testing puproses
print_r($json);
// re-encode 
$json = json_encode($json);

echo $json;
  • {"total":"100","page":"1","records":"100","rows":[{"no":"1","part_number":"2","part_name":"3","price":"4","test1":"5","test2":"7","note":"8"}],"test1":"5","test2":"7"} its your final output.does not match to what is wanted – sharif2008 Jun 19 '13 at 16:33
  • it should be like this: {"total":"100","page":"1","records":"100","rows":[{"no":"1","part_number":"2","p‌​art_name":"3","price":"4","test1":"5","test2":"7","note":"8"}]} not like this: {"total":"100","page":"1","records":"100","rows":[{"no":"1","part_number":"2","p‌​art_name":"3","price":"4","note":"8"}],"test1":"5","test2‌​":"7"} – Rio Eduardo Jun 19 '13 at 16:34
  • yeah sharif that's the output but not match to what is wanted, can you help me? – Rio Eduardo Jun 19 '13 at 16:36
  • yeah @RioEduardo .i also mentioned this. – sharif2008 Jun 19 '13 at 16:37
  • @user20232359723568423 - nah, that's the answer, thank you so much for helping :) – Rio Eduardo Jun 19 '13 at 16:42
  • @user20232359723568423357842364 i have alos tried like this.here is your ans. – sharif2008 Jun 19 '13 at 16:42
  • @user20232359723568423357842364 - one more, how about in javascript? can you give me an example? :) – Rio Eduardo Jun 19 '13 at 17:26
  • @user20232359723568423357842364 - http://stackoverflow.com/questions/17216632/how-to-add-attribute-in-json-in-javascript – Rio Eduardo Jun 20 '13 at 14:41