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?
array
, not astdObject
? In that case you can access the name via$p2->name
– thaJeztah Mar 30 at 18:21