Reputation: 609
I'm trying to parse a JSON object in PHP. My efforts have lead to ending up with this var_dump:
string(5) "Malta"
string(2) "mt"
string(11) "St Julian's"
string(5) "Malta"
string(2) "mt"
string(11) "St Julian's"
Now what I want is just to print "Malta". All efforts of trying have failed and I need a hint into what to do next. Any ideas of how i can go about this? Thanks
Trying to parse this part: "location":
{
"totalFound":2,
"content":[
{
"id":"a1d17bwqeqewqeqweaf-1e54-4861-92e1-8246baba11d6",
"title":"Developer",
"refNumber":"REFqweqwe4N",
"createdOn":"2015-08-28T11:10:07.000Z",
"updatedOn":"2015-08-28T13:19:59.000Z",
"location":{
"country":"Malta",
"countryCode":"mt",
"city":"St Julian's"
},
"status":"SOURCING",
"actions":{
"details":{
"url":"www.google.com",
"method":"GET"
}
}
}
],
"offset":0,
"limit":10
}
My code snippet:
$jfo = json_decode($data, TRUE);
foreach ($jfo['content'] as $category) {
if (isset($category['title']) != null) {
}
if (isset($category['location']) != null) {
foreach ($category['location'] as $location){
var_dump($location);
print_r($location);
Upvotes: 1
Views: 63
Reputation: 12826
The issue is that you're iterating over each element in location
, i.e. country
, countryCode
, and city
.
This does what you need:
$jfo = json_decode($data, TRUE);
foreach ($jfo['content'] as $content)
{
echo $content['location']['country'];
}
Upvotes: 1
Reputation: 375
try to echo $category['location']['country']
remove the for each on there
its tying to loop your location array
Upvotes: 1