I am trying to display some values from my JSON but fail on two whereas the rest works perfectly. I suspect it has something to do with the characters within, but have no idea on how to proceed.
Tried tricks:
- UTF-8-ing everything
- Regexing the JSON directly
- Decode, Encode with JSON_PRETTY_PRINT/JSON_UNESCAPED_UNICODE and Decode again
- and some more I can't remember anymore...
Any pointers on how to tackle this greatly appreciated:
var_dump of $json
{
"products":{
"product":[{
"@id":"1",
"name":"First Name",
"fetchers":{
"fetcher":[{
"@fetcherId":"1",
"link1":"http:\/\/www.example.com\/pv\/?1&NAME=[[first-name\/1\/?refID=1\/first-name]]&denk5=[[1]]",
"link2":"http:\/\/www.example.com\/pc\/?1&NAME=[[first-name\/1\/?refID=1\/first-name]]&denk5=[[1]]"
}]
}
},{
"@id":"2",
"name":"Second Name",
"fetchers":{
"fetcher":[{
"@fetcherId":"2",
"link1":"http:\/\/www.example.com\/pv\/?1&NAME=[[second-name\/2\/?refID=2\/second-name]]&denk5=[[2]]",
"link2":"http:\/\/www.example.com\/pc\/?1&NAME=[[second-name\/2\/?refID=2\/second-name]]&denk5=[[2]]"
}]
}
}]
}
}
PHP Code
<?php
$json = json_decode($json,true);
foreach($json['products']['product'] as $data) {
echo $data['@id'].'<br/>';
echo $data['name'].'<br/>';
echo $data['fetchers']['fetcher']['@fetcherId'].'<br/>';
echo $data['fetchers']['fetcher']['link1'].'<br/>';
echo $data['fetchers']['fetcher']['link2'].'<br/>';
}
Expected result
1
First Name
1
http://www.example.com/pv/?1&NAME=[[first-name/1/?refID=1/first-name]]&denk5=[[1]]
http://www.example.com/pc/?1&NAME=[[first-name/1/?refID=1/first-name]]&denk5=[[1]]
2
Second Name
2
http://www.example.com/pv/?1&NAME=[[second-name/2/?refID=2/second-name]]&denk5=[[2]]
http://www.example.com/pc/?1&NAME=[[second-name/2/?refID=2/second-name]]&denk5=[[2]]
What I get
1
First Name
1
<- link1 empty
<- link2 empty
2
Second Name
2
<- link1 empty
<- link2 empty
What am I missing?
EDIT @Brad solution works perfectly.