I have some JSON stored in a database. Please assume I have a good reason for doing so. I then retrieved it from the DB and wish to include it in another JSON string. The following treats the JSON from the DB as standard strings instead of JSON as I desire. Will I need to loop over $json_from_db and first convert the cmd values to arrays/objects, or is there a better way?
<?php
$json_from_db=array(
array('id'=>1,'cmd'=>'{"a":{}}'),
array('id'=>3,'cmd'=>'{"b":{"name":"x","value":"xxx"}}'),
array('id'=>5,'cmd'=>'{"b":{"name":"y","value":"yyy"}}'),
array('id'=>9,'cmd'=>'{"c":{"name":"z","extra":"hello","more"=>1}}'),
);
$arr=array('a'=>"hello",'json'=>array('a'=>"hello"),'json_from_db'=>$json_from_db);
$json=json_encode($arr);
echo($json."\n\n");
print_r(json_decode($json));
OUTPUT
{"a":"hello","json":{"a":"hello"},"json_from_db":[{"id":1,"cmd":"\"a\":{}"},{"id":3,"cmd":"\"b\":{\"name\":\"x\",\"value\":\"xxx\"}"},{"id":5,"cmd":"\"b\":{\"name\":\"y\",\"value\":\"yyy\"}"},{"id":9,"cmd":"\"c\":{\"name\":\"z\",\"extra\":\"hello\",\"more\"=>1}"}]}
stdClass Object
(
[a] => hello
[json] => stdClass Object
(
[a] => hello
)
[json_from_db] => Array
(
[0] => stdClass Object
(
[id] => 1
[cmd] => "a":{}
)
[1] => stdClass Object
(
[id] => 3
[cmd] => "b":{"name":"x","value":"xxx"}
)
[2] => stdClass Object
(
[id] => 5
[cmd] => "b":{"name":"y","value":"yyy"}
)
[3] => stdClass Object
(
[id] => 9
[cmd] => "c":{"name":"z","extra":"hello","more"=>1}
)
)
)
Please assume I have a good reason for doing soyou know you doing something wrong!