php数组有两个相同的键

时间:2013-06-07 09:32:58

标签: php facebook-graph-api

我有两个id。一个是Facebook id的{​​{1}},第二个是Facebook好友的ID。我想将它们存储在两个数据库中。评论comment中的idPost表中的friend id。我面临的问题是“id”值被覆盖。

检索数据:

User

代码:

{
   "data": [
  {
     "id": "aaaaaaaaaaaaaaaa",
     "from": {
        "name": "xxxx xxxx",
        "id": "111111111"
     },
     "message": "fetch this comment :P",
     "can_remove": true,
     "created_time": "2013-06-05T09:00:00+0000",
     "like_count": 0,
     "user_likes": false
  },
  {
     "id": "aaaaaaaaaaaaaaaa",
     "from": {
        "name": "xxxxxxxxx",
        "id": "111111111"
     },
     "message": "Fetching comments.",
     "can_remove": true,
     "created_time": "2013-06-05T17:22:34+0000",
     "like_count": 0,
     "user_likes": false
  },
  {
     "id": "aaaaaaaaaaaaaaaa",
     "from": {
        "name": "xxxxxxxxx",
        "id": "11111111111"
     },
     "message": "?",
     "can_remove": true,
     "created_time": "2013-06-05T17:27:55+0000",
     "like_count": 0,
     "user_likes": false
  },

1 个答案:

答案 0 :(得分:0)

由于您正在使用迭代器对象,因此您需要确保区分不同级别的ID键的方法,因为它们看起来都与此处所示相同

foreach ($jsonIterator as $key => $val) {
    if(!is_array($val) && $key == 'id') {
        echo "$key => $val\n";
    }
}

相反
foreach ($jsonIterator as $key => $val) {
    if($key === 'from') {
        echo $val['id'] . "\n";
    }
}

在你的情况下它应该是

foreach ($jsonIterator as $key => $val) {
    if($key === 'from') {
        if (array_key_exists('id', $val)) {
            echo $val['id'] . "\n";
            // Process
            // $user_obj->id = $val['id'];
            // $flag_usr = $flag_usr + 1;
        }
    }
}