Reputation: 2275
I've been having issues trying to parse this json data.
I tried to do this to return the name but its not working:
foreach(json_decode($test) as $item){
$name= $item->users->name;}
This is the json code:
{
"users":[
{
"id":"dsfdfsd",
"id_str":"dsfsdf",
"name":"Davy",
"screen_name":"Davy232",
"location":"Colorado"
},
{
"id":"wer",
"id_str":"wer",
"name":"Sarah",
"screen_name":"Davy232",
"location":"LA"
},
{
"id":"fdf",
"id_str":"fdf",
"name":"James",
"screen_name":"James374",
"location":"Vegas"
}
]
}
Upvotes: 2
Views: 51
Reputation: 68486
That is because the JSON is invalid , Here's the proper fixed JSON
{
"users":[
{
"id":"dsfdfsd",
"id_str":"dsfsdf",
"name":"Davy",
"screen_name":"Davy232",
"location":"Colorado"
}
]
}
What were the problems ?
dsfdfsd
with doubles quotes.Also, your foreach
should be like this..
foreach(json_decode($test) as $item){
echo $item[0]->name;
}
Working Demo - Part 1 Working Demo - Part 2
Upvotes: 2