1

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);
8
  • can i get the print_r of that object ? Commented Aug 28, 2015 at 15:43
  • Looks more like the var_dump is inside a loop rather than of the decoded object? Commented Aug 28, 2015 at 15:43
  • @jon yes i think so too Commented Aug 28, 2015 at 15:44
  • Result of print_r: string(5) "Malta" Maltastring(2) "mt" mtstring(11) "St Julian's" St Julian'sstring(5) "Malta" Maltastring(2) "mt" mtstring(11) "St Julian's" St Julian's Commented Aug 28, 2015 at 15:45
  • your json isn't right according to jsonlint Commented Aug 28, 2015 at 15:51

2 Answers 2

1

try to echo $category['location']['country'] remove the for each on there its tying to loop your location array

Sign up to request clarification or add additional context in comments.

1 Comment

tried, didn't work - its only printing the first character
1

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'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.