0

我有两个id。一个是idFacebook 的comment,第二个是 Facebook 朋友的 id。我想将这些存储在两个数据库中。id在表Post中评论friend idUser我面临的问题是“id”值被覆盖了。

检索数据:

{
   "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
  },

代码 :

foreach($jsonIterator as $key => $val) {
echo "$key: => $val";
                $post_obj_insert->user_id = $question->user_id;
                if($key=='from'){
                    $user_obj->where('FacebookId', $val)->get();
                    if($user_obj->FacebookId=''){
                        if($key=='name'){
                            $user_obj->name = $val;
                            $flag_usr = $flag_usr + 1; 
                        }
                        if($key=='id'){
                            $user_obj->id = $val;
                            $flag_usr = $flag_usr + 1; 
                        }
                        $user_obj->FacebookId = $val;   
                        $flag_usr = $flag_usr + 1;
                    }
                    if($flag_usr==2){
                        $user_obj->isRegistered=0;
                        $user_obj->save();  
                        $user_obj = new user();
                    }
                }

                else if($key=='message'){
                    $post_obj_insert->post_text = $val;
                    $flag=$flag+1;
                }
                else if($key=='like_count'){
                    $post_obj_insert->count = $val;
                    $flag=$flag+1;
                }
                else if($key=='id'){
                    $post_obj_insert->facebook_post_id = $val;
                    $flag=$flag+1;
                }
                if($flag==3){
                    $post_obj_insert->save();
                    $flag=0;
                    $post_obj_insert = new Post();
                }           
            }
4

1 回答 1

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;
        }
    }
}
于 2013-06-08T16:50:58.390 回答