php - 过滤和合并两个二维数组之间的数据 - 结果不得显示在原始数组中多次出现的行

标签 php arrays multidimensional-array merge filtering

如何从下面两个数组中获取元素,其中 email 键的值仅在任一列表中存在一次,然后合并它们的键?

比如说,我有两个数组:

$arr1 = [
    ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d2d2f3d4ded2dadf9dd0dcde" rel="noreferrer noopener nofollow">[email protected]</a>', 'name' => 'John Doe'],
    ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f1d1d3f18121e1613511c1012" rel="noreferrer noopener nofollow">[email protected]</a>', 'name' => 'Johnny Sins'],
    ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdaeae8daaa0aca4a1e3aea2a0" rel="noreferrer noopener nofollow">[email protected]</a>', 'name' => 'Jose Alvarado']
];

$arr2 = [
    ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="43202003242e222a2f6d202c2e" rel="noreferrer noopener nofollow">[email protected]</a>', 'country' => 'Japan'],
    ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="accfcfeccbc1cdc5c082cfc3c1" rel="noreferrer noopener nofollow">[email protected]</a>', 'country' => 'China'],
    ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d5f5f7d5a505c5451135e5250" rel="noreferrer noopener nofollow">[email protected]</a>', 'country' => 'Korea'],   
];

最终结果应该是:

[
     ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c1d1d3c1b111d1510521f1311" rel="noreferrer noopener nofollow">[email protected]</a>', 'name'  => 'John Doe'],
     ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f69494b6919b979f9ad895999b" rel="noreferrer noopener nofollow">[email protected]</a>', 'name'  => 'Johnny Sins', 'country' => 'Korea'],
];

我尝试了以下操作,但我坚持合并 key :

$merged = array_merge($arr1, $arr2);

$result = array_filter($merged, function($value) use($merged) {
    return array_count_values(array_column($merged, 'email'))[$value['email']] < 3;
});

最佳答案

我在此演示中使用的方法是首先确定两个阵列上是否存在重复的电子邮件。此类检查将生成一个包含所有电子邮件的数组,这些电子邮件不应被视为来自两个数组的重复电子邮件的边界。

然后,您可以在过滤第一个和第二个数组时使用此类数组,以便仅返回上述引用的不允许电子邮件数组中不包含任何电子邮件的条目。

最终的问题是返回合并后的过滤数组。

引用文献:

https://www.php.net/manual/en/function.array-column

https://www.php.net/manual/en/function.array-count-values

https://www.php.net/manual/en/function.array-filter

https://www.php.net/manual/en/function.array-keys

https://www.php.net/manual/en/function.in-array

https://www.php.net/manual/en/function.array-merge

演示:

https://onlinephp.io/c/c638f

<?php

$arr1 = [
    ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d1c1c3d1a101c1411531e1210" rel="noreferrer noopener nofollow">[email protected]</a>', 'name' => 'John Doe',],
    ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f99b9bb99e94989095d79a9694" rel="noreferrer noopener nofollow">[email protected]</a>', 'name' => 'Johnny Sins',],
    ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="791a1a391e14181015571a1614" rel="noreferrer noopener nofollow">[email protected]</a>', 'name' => 'Jose Alvarado',],
];

$arr2 = [
    ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="482b2b082f25292124662b2725" rel="noreferrer noopener nofollow">[email protected]</a>', 'country' => 'Japan',],
    ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4a7a784a3a9a5ada8eaa7aba9" rel="noreferrer noopener nofollow">[email protected]</a>', 'country' => 'China',],
    ['email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b49496b4c464a424705484446" rel="noreferrer noopener nofollow">[email protected]</a>', 'country' => 'Korea',],   
];

//returns an array with emails being duplicated in $arr
function getDuplicatedEmails($arr){
    //gets list of emails found from entries in $arr
    $emails = array_column($arr, 'email');    
    //gets how many time each email is occurring in $emails
    $emailsWithCounts = array_count_values($emails);
    //filteres the above array returning only values with repetitions
    $duplicates = array_filter($emailsWithCounts, function($value){ return $value > 1; });
    
    //returns the emails found
    return array_keys($duplicates);
}

//returns an array like $arr minus the entries having [email] included in $notAllowed
function filterByEmailNotAllowed($arr, $notAllowed = []){
    //filter the list of emails
    $filtered = array_filter($arr, function ($item) use ($notAllowed) {
        //return this item if the email is not included in $notAllowed
        return !in_array($item['email'], $notAllowed);
    });  
    return $filtered;
}

//merge the arrays arr1 and arr2 excluding entries having emails duplicated in any of the two input arrays
function merge($arr1, $arr2){
    $emailNotAllowed1 = getDuplicatedEmails($arr1);
    $emailNotAllowed2 = getDuplicatedEmails($arr2);
    $emailNotAllowed = array_merge($emailNotAllowed1, $emailNotAllowed2);
    
    $filter1 = filterByEmailNotAllowed($arr1, $emailNotAllowed);
    $filter2 = filterByEmailNotAllowed($arr2, $emailNotAllowed);
    $filter = array_merge($filter1, $filter2);    
    
    return $filter;
}

$result = merge($arr1, $arr2);
var_dump($result);

关于php - 过滤和合并两个二维数组之间的数据 - 结果不得显示在原始数组中多次出现的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75210264/

相关文章:

javascript - 使用已删除的值创建新数组(React Native)

php - 在PHP中递归创建多维数组

php - Doctrine 命名查询 : Specifing limit on query call

php - 调试长时间运行的 PHP 脚本

arrays - 修改字典中的 Swift 数组<String, AnyObject>

c++在转换中为多数组错误添加值(value)

java - 从多维数组中删除重复项的最佳方法?

javascript - 二维 javascript 数组中只有特定的 'columns'

php - 更新 MySQL 错误但代码没有问题?

php - 设置变量时显示 div 并让它在 x 秒后消失