My goal is to loop through each Item within the JSON and add an additional key called "modal" to each "Item", and the value paired with that key that would contain html. I stripped down my code below.
I'm able to add 'modal' to each 'Item', however the value somehow gets set to null, instead of the html I would like it to be.
JSON file:
{"Item":{
"thumbnail":"http://...",
"title": "Item title"
},
"Item":{
"thumbnail":"http://...",
"title": "Item title"
}}
php:
$json_a=json_decode($json, true);
foreach ($json_a['Item'] as &$obj){
$out = '<img src=\"' . $obj['thumbnail'] . '\">';
$out .= '<span>' . $obj['title'] . '</span>';
$obj['modal'] = $out; //value of $out doesn't get passed in, instead the value becomes null.
}
$json_a=json_encode($json_a);
print_r($json_a);
json output:
...{'modal': null}...
Edited the JSON to be valid. It comes from Amazon's product API, I shortened it for this example and made it invalid.
json??? Its not valid