how can i loop through a json array requested from an api?
Question
im requesting information from the instagram api in php like this:
<?php $relation = $instagram->get('users/'.$item->id.'/relationship');
..
which return this json data array for me:
object(stdClass)#58(2){
[
"meta"
]=>object(stdClass)#59(1){
[
"code"
]=>int(200)
}[
"data"
]=>object(stdClass)#60(3){
[
"outgoing_status"
]=>string(7)"follows"[
"target_user_is_private"
]=>bool(true)[
"incoming_status"
]=>string(4)"none"
}
}
note: i used var_dump($relation) to bring this out
what I'm trying to do is loop through this array and display the outgoing status and the incoming status i.e
loop(json-array){
echo outgoing_status;
echo incoming_status;
}
thank you very much..
Solution
You have an object (instance of stdClass
, the generic object), not an array.
$outgoing_status = $response->data->outgoing_status;
$incoming_status = $response->data->incoming_status;
As a side note, use json_decode($json, TRUE)
to return the data as an associative array instead of an object.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow