0

I have been trying to parse a json array with no success. I can get a root element but not any array elements. Below is the beginning of my json array from Foursquare which has re-occurring venue elements.

 response: {
 keywords: {}
 suggestedRadius: 10000
 headerLocation: "here"
 headerFullLocation: "here"
 headerLocationGranularity: "unknown"
 headerMessage: "Suggestions for Friday evening"
 totalResults: 214
 groups: [
 {
 type: "Recommended Places"
 name: "recommended"
 items: [
 {
 reasons: {
 count: 0
 items: [ ]
 }
 venue: {
 id: "4b799a05f964a520b1042fe3"
 name: "Green Gables"
 contact: {
 phone: "3097472496"
 formattedPhone: "(309) 747-2496"
 }
 location: {
 address: "17485 East 2500 North Rd"

Below is my PHP code to try to get the name of the restaurants.

 $obj = json_decode($uri, true);
 foreach($obj['response']['groups'] as $p)
 {
 if(isset($p['items']))
 {
 foreach($p['items'] as $p1)
 {
 if(isset($p1['venue']))
 {
// echo varDumpToString($p1['venue']); // this dump works ok and shows the elements
 foreach($p1['venue'] as $p2)
 {
 echo varDumpToString($p2['name']); // This is where I get the error
 }
 }
 }
 } 
 }

The program drills down to the name element and then gives me an error saying "Unefined index: name" and also 'Illegal string offset name". This message appears 14 times which is one time for each item in the array. So why is "name" not recognized?

asked Mar 30, 2013 at 18:17
3
  • When you decoded your json using json_decode(), did you return it as an array or object? If object, try using $p2->name. Commented Mar 30, 2013 at 18:21
  • Are you sure that node is converted to an array, not a stdObject? In that case you can access the name via $p2->name Commented Mar 30, 2013 at 18:21
  • LOL @AdrianCrepaz beat me to it Commented Mar 30, 2013 at 18:22

2 Answers 2

2

response.groups[x].items[y].venue is not an array. You are currently trying to access response.groups[x].items[y].venue[z].name, but you are actually accessing response.groups[x].items[y].venue.id.name, which does not exist. That last foreach iterates over the venue properties, not the venues. There is only one venue for each item.

This is what it should look like:

$obj = json_decode($uri, true);
foreach ($obj['response']['groups'] as $group) {
 if (isset($group['items'])) {
 foreach ($group['items'] as $item) {
 if (isset($item['venue'])) {
 echo varDumpToString($item['venue']['name']);
 }
 }
 }
}
answered Mar 30, 2013 at 18:21

5 Comments

So what should my last foreach look like? I tried about every combination I can think of. Why am I accessing venue.id.name. I thought id and name were separate elements. I just want to get id and name as separate items.
@Dave - Check out my example. There is only a single venue for each item, so you cannot iterate them. You should also try to use more descriptive names for your variables to improve readability. This makes it a lot easier to catch errors like this.
If I do 'echo varDumpToString($p2['venu']['name'] I will get the name but it repeats itself 14 times for each venue. How can I just get the name as a singleton?
@Dave - I am not sure what exactly you are doing, but I suspect the following: If all items have the same venue, the name is obviously displayed multiple times.
@elusive - Thanks very much for your suggestion. It worked great! I am a newbie to PHP and it was impossible to find an example of what I was trying to do. Now I know and I really appreciate your help.
0

Try changing this line,

 foreach($p1['venue'] as $p2)

to

 foreach($p1['venue'] as $p2Key => $p2)
answered Mar 30, 2013 at 18:25

1 Comment

This is not going to help. $p2 will contain 4b799a05f964a520b1042fe3 in the first case, so accessing the name property of that string does not make a lot of sense. This is exactly why OPs code does not work.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.