我想知道"对于(x in y)" PHP中的JavaScript?我从其他网站上看到过这个代码与我程序的功能相同,但是我想把它转换成php。
喜欢这个javascript代码:
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);
})
}
我已经尝试过这样做,但它不起作用:
obj = data['data'];
for($x in obj){
}
答案 0 :(得分:1)
查看PHP文档 foreach 循环:
.sharedaddy ul li {
background: none;
}
答案 1 :(得分:0)
对于关联数组,它是
foreach($array as $key => $value) {
...
}
如果你有一个对象,你可以用get_object_vars
得到一个公共属性的关联数组,所以你可以这样写:
foreach (get_object_vars($obj) as $key => $value) {
...
}