I am wondering what is the equivalent of "for in(x in y)" of javascript in PHP? I've seen this code from other website with the same functionalities of my program but i want it to be converted into php.
Like for this javascript code:
function get_tat_friends(){
var ar_friends = []; ar_fr_info = [];
FB.api('me/photos?type=uploaded&fields=likes,comments',function(data){
obj = data['data'];
for(x in obj){
if(obj[x]['likes'] != null){
obj_like = obj[x]['likes']['data'];
for(y in obj_like){
if(ar_friends[obj_like[y]['id']] == null){
ar_friends[obj_like[y]['id']] = 1;
ar_fr_info[obj_like[y]['id']] = obj_like[y]['name'];
}else{
ar_friends[obj_like[y]['id']] += 1;
ar_fr_info[obj_like[y]['id']] = obj_like[y]['name'];
}
}
}
if(obj[x]['comments'] != null){
obj_like = obj[x]['comments']['data'];
for(y in obj_like){
if(ar_friends[obj_like[y]['from']['id']] == null){
ar_friends[obj_like[y]['from']['id']] = 1;
ar_fr_info[obj_like[y]['from']['id']] = obj_like[y]['from']['name'];
}else{
ar_friends[obj_like[y]['from']['id']] += 2;
ar_fr_info[obj_like[y]['from']['id']] = obj_like[y]['from']['name'];
}
}
}
}
ar_friends = getSortedKeys(ar_friends);
//delete user_id
var index = ar_friends.indexOf(user_id);
if (index >= 0) {
ar_friends.splice( index, 1 );
}
//call done function
done_load_friends(ar_friends,ar_fr_info);
})
}
I already tried to do this but it doesn't work:
obj = data['data'];
for($x in obj){
}
Take a look at PHP documentation foreach loop:
foreach(array_expression as $value){
statement
}
For an associative array, it's
foreach($array as $key => $value) {
...
}
If you have an object, you can get an associative array of its public properties with get_object_vars
, so you can write:
foreach (get_object_vars($obj) as $key => $value) {
...
}