我有 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/