I’m struggle extracting necessary data from this json stored inside php variable.
Not yet expert in this kind of data structure yet…
Wondering anyone know how to make it work.
I need to loop through results, then read each contact to get
1. the value where type=email
2. value->givenName where type=name (whenever element exist)
the json decoded value is as below
$aNative = json_decode($retval);
echo "lang: " . $aNative->query->lang . "<br>"; (works)
(when go deeper, fail to retrieve any value...)
echo "created: " . $aNative->results->contact[0]->created . "<br>";
foreach($aNative->$query->results as $contact)
{
echo $contact->id;
echo $contact->fields[0]->type;
if($contact->fields[0]->type=="email")
{
echo $contact->fields[0]->value;
}
}
full json:
{
"query":{
"count":10,
"created":"2012-06-13T09:13:45Z",
"lang":"en-US",
"results":{
"contact":[
{
"created":"2006-10-29T04:32:07Z",
"updated":"2006-10-29T04:32:07Z",
"uri":"http://social.yahooapis.com/v1/user/BNQJOMYDQYLY2FZSMROV4QDA6MI/contact/14",
"isConnection":"false",
"id":"14",
"fields":{
"created":"2006-10-29T04:32:07Z",
"updated":"2006-10-29T04:32:07Z",
"uri":"http://social.yahooapis.com/v1/user/BNQJOMYDQYLY2FZSMROV4QDA6MI/contact/14/email/11",
"id":"11",
"type":"email",
"value":"angBB@gmail.com",
"editedBy":"OWNER"
}
},
{
"created":"2006-10-29T04:32:07Z",
"updated":"2006-10-29T04:32:07Z",
"uri":"http://social.yahooapis.com/v1/user/MNQJOMYDQYLY2FZSMROV4QDA6MI/contact/12",
"isConnection":"false",
"id":"12",
"fields":[
{
"created":"2006-10-29T04:32:07Z",
"updated":"2006-10-29T04:32:07Z",
"uri":"http://social.yahooapis.com/v1/user/MNQJOMYDQYLY2FZSMROV4QDA6MI/contact/12/email/6",
"id":"6",
"type":"email",
"value":"jaq@lifemastr.com",
"editedBy":"OWNER"
},
{
"created":"2006-10-29T04:32:07Z",
"updated":"2006-10-29T04:32:07Z",
"uri":"http://social.yahooapis.com/v1/user/MNQJOMYDQYLY2FZSMROV4QDA6MI/contact/12/name/5",
"id":"5",
"type":"name",
"value":{
"givenName":"'Jaq'",
"middleName":null,
"familyName":null,
"prefix":null,
"suffix":null,
"givenNameSound":null,
"familyNameSound":null
},
"editedBy":"OWNER"
}
]
}
]
}
}
}
You have forgotten about
query. You have to access to results for example in this way:And review whole your code you are trying to access to wrong properties many times. For example in foreach you shall use
$aNative->query->results->contactinstead of$aNative->resultsbecauseresultsit is an object and not array.