两个Foreach循环比较解决方案

0 php instagram

我有两个数组:

第一:

"data":  [
     {
      "username": "ishaza_",
      "bio": "All photos by me:$",
      "website": "",
      "full_name": "Shaza",
      "id": "411073318"
    },
     {
      "username": "taylovesrissa",
      "bio": "",
      "website": "",

      "full_name": "Taylor Angell",
      "id": "750547868"
    },
     {
      "username": "a_sh997",
      "bio": "",
      "website": "",

      "full_name": "Ahmed",
      "id": "679088716"
    }
]
Run Code Online (Sandbox Code Playgroud)

第二个:

"data":  [
     {
      "username": "ishaza_",
      "bio": "All photos by me:$",
      "website": "",

      "full_name": "Shaza",
      "id": "411073318"
    },
     {
      "username": "taylovesrissa",
      "bio": "",
      "website": "",

      "full_name": "Taylor Angell",
      "id": "750547868"
    },
     {
      "username": "a_sh997",
      "bio": "",
      "website": "",

      "full_name": "Ahmed",
      "id": "6790887163"
    }
]
Run Code Online (Sandbox Code Playgroud)

我想打印那些第一个和第二个都没有的数据.

我使用了一个foreach循环,但它给出了异常的结果,就像每个项目被多次打印一样.

我的代码是:

foreach ($UserFollows->data as $entry1) {

    foreach ($UserFollower->data as $entry2) {

        $e1=$entry1->id;
        $e2=$entry2->id;
        if($e1!=$e2) {
            print''.$e1.'</br>';
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

这里$UserFollows$UserFollower代表两个数组.这个数组来自instagram API.实际上我想比较两个api的instagram一个是跟随和其他后续,并想要打印那些未列在api结果中的数据.

但我无法理解为什么我的代码返回foreach循环的异常结果

xda*_*azz 5

使用array_uintersect计算数组的交集,通过回调函数比较数据.

$result = array_uintersect($UserFollows->data, $UserFollower->data, function($a, $b) {
  return strcmp($a->id, $b->id);
});
Run Code Online (Sandbox Code Playgroud)