php - 使用 array_diff 比较多维数组

标签 php arrays

我有 2 个数组,然后我想通过电子邮件 key 过滤它们。像这样

数组1

array (size=5)
  0 => 
     array (size=3)
        'name' => string 'Client 1' (length=8)
        'email' => string '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe9d92979b908acfbe99939f9792d09d9193" rel="noreferrer noopener nofollow">[email protected]</a>' (length=17)
  1 => 
     array (size=3)
        'name' => string 'Client 2' (length=8)
        'email' => string '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0d3dcd9d5dec482f0d7ddd1d9dc9ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>' (length=17)

数组2

array (size=3)
   0 => 
      array (size=4)
        'name' => string 'Client 3' (length=8)
        'email' => string '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8be8e7e2eee5ffb9cbece6eae2e7a5e8e4e6" rel="noreferrer noopener nofollow">[email protected]</a>' (length=17)
        'role_id' => float 3
   1 => 
      array (size=4)
        'name' => string 'Client 6' (length=8)
        'email' => string '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="62010e0b070c165422050f030b0e4c010d0f" rel="noreferrer noopener nofollow">[email protected]</a>' (length=17)
        'role_id' => float 2
   2 => 
      array (size=4)
        'name' => string 'Client 7' (length=8)
        'email' => string '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7516191c101b0142351218141c195b161a18" rel="noreferrer noopener nofollow">[email protected]</a>' (length=17)
        'role_id' => float 3

我想过滤掉类似的电子邮件项目。结果如何返回如下:

array (size=2)
   0 => 
      array (size=4)
        'name' => string 'Client 6' (length=8)
        'email' => string '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="43202f2a262d377503242e222a2f6d202c2e" rel="noreferrer noopener nofollow">[email protected]</a>' (length=17)
        'role_id' => float 2
   1 => 
      array (size=4)
        'name' => string 'Client 7' (length=8)
        'email' => string '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d7e71747873692a5d7a707c7471337e7270" rel="noreferrer noopener nofollow">[email protected]</a>' (length=17)
        'role_id' => float 3

最佳答案

您可以使用array_udiff .

If 将通过将第一个数组的元素与使用给定回调传递给 array_udiff 的其他数组的元素进行比较来过滤第一个数组。当回调针对一对返回 0 时,该元素将从结果中删除。

$result = array_udiff($arr2, $arr1, function ($a, $b) {
    return strcmp($a['email'], $b['email']);
});

关于php - 使用 array_diff 比较多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36667607/

相关文章:

c++ - 我们可以用 SFML/C++ 制作 Sprite 数组吗?

c - 如何在不更改原始数组或分配新数组的情况下对两个数组进行排序?

PHP 困惑检测器给出误报

php - 使用 PHP 添加/编辑 XML 文件的标签

Java - 如何在我的程序中为我的每个成绩显示一个字母等级,而不仅仅是我的最后一个?

c# - 如何获取存储在内存中的可执行文件的 FileVersion 信息作为字节数组?

javascript - 使用 lodash 按可选属性对 javascript 数组进行排序

php - 尝试使用 PNG 和 JPEG 支持构建 PHP 和 GD

php - 如何从 Facebook 个人资料或页面 URL 获取数字 ID? PHP,Facebook 图形 API

php - 通过 JSON 在 JS 中使用 PHP 方法?