如何在PHP中将两个数组元素相互匹配/配对并仅返回一个

是否有可能匹配所有并保留一个?例如,是匹配项,或将在筛选的数组中删除。通过所有元素进行“交叉匹配”是可以的。email_adrad_owner_email[0][1][0][1]["email_adr"]["ad_owner_email"]


 array(3) {

      [0]=>

      array(3) {

        ["id"]=>

        string(5) "14598"

        ["email_adr"]=>

        string(14) "jos@pl.nuu"

        ["ad_owner_email"]=>

        string(23) "boo@gmail.qom"

      }

      [1]=>

      array(3) {

        ["id"]=>

        string(5) "14598"

        ["email_adr"]=>

        string(23) "boo@gmail.qom"

        ["ad_owner_email"]=>

        string(14) "jos@pl.nuu"

      }

      [2]=>

      array(3) {

        ["id"]=>

        string(5) "14598"

        ["email_adr"]=>

        string(23) "boo@gmail.qom"

        ["ad_owner_email"]=>

        string(21) "pelle@med.nuu"

      }

    }

期望结果:

 array(2) {

          [0]=>

          array(3) {

            ["id"]=>

            string(5) "14598"

            ["email_adr"]=>

            string(23) "boo@gmail.qom"

            ["ad_owner_email"]=>

            string(14) "jos@pl.nuu"

          }

          [1]=>

          array(3) {

            ["id"]=>

            string(5) "14598"

            ["email_adr"]=>

            string(23) "boo@gmail.qom"

            ["ad_owner_email"]=>

            string(21) "pelle@med.nuu"

          }

        }


肥皂起泡泡
浏览 160回答 2
2回答

守着一只汪

在第一步,您可以将所有可能的电子邮件对收集到数组中:$tmpforeach($array as $record){&nbsp; &nbsp; $tmp1[$record['email_adr']][] = $record['ad_owner_email'];&nbsp;}这将创建如下数组:Array(&nbsp; &nbsp; [jos@pl.nuu] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => boo@gmail.qom&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [boo@gmail.qom] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => pelle@med.nuu&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => jos@pl.nuu&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp;)现在,您可以使用进一步的逻辑:foreach($array as $ind=>$record){&nbsp; &nbsp; if (isset($tmp1[$record['ad_owner_email']]) && !empty($tmp1[$record['ad_owner_email']]) )&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (in_array($record['email_adr'],$tmp1[$record['ad_owner_email']]))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // index of current value in $tmp1 array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tm1 = array_search($record['ad_owner_email'],$tmp1[$record['email_adr']]);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // delete an element from $tmp1 and from $array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($tmp1[$record['email_adr']][$tm1]);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($array[$ind]);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}&nbsp;&nbsp;编辑根据以下要求$array = [["id"=>"111","email_adr"=>"AAA","ad_owner_email"=>"BBB"],&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ["id"=>"111","email_adr"=>"BBB","ad_owner_email"=>"CCC"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ["id"=>"111","email_adr"=>"BBB","ad_owner_email"=>"AAA"], <--discarded&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ["id"=>"500","email_adr"=>"QQQ","ad_owner_email"=>"PPP"],&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ["id"=>"500","email_adr"=>"PPP","ad_owner_email"=>"QQQ"], <--discarded&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ["id"=>"1000","email_adr"=>"PPP","ad_owner_email"=>"QQQ"], <-saved because id=1000 and differs from id=500&nbsp;&nbsp; &nbsp; ];--------==::Desired result::==-----------Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 111&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [email_adr] => AAA&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ad_owner_email] => BBB&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 111&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [email_adr] => BBB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ad_owner_email] => CCC&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [2] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 500&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [email_adr] => QQQ&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ad_owner_email] => PPP&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;[3] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 1000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [email_adr] => PPP&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ad_owner_email] => QQQ&nbsp; &nbsp; &nbsp; &nbsp; ))您可以使用 next 逻辑而不是上一个逻辑:// removes duplicate elements from multidimentional array$array = array_unique($array, SORT_REGULAR);// foreach($array ...) //<-- creating an array $tmp1foreach($array as $ind=>$record){&nbsp; &nbsp; $id&nbsp; &nbsp; &nbsp; &nbsp;= $record['id'];&nbsp; &nbsp; $em_owner = $record['ad_owner_email'];&nbsp; &nbsp; $em_adr&nbsp; &nbsp;= $record['email_adr'];&nbsp; &nbsp; if (isset( $tmp1[$id][$em_owner] ) &&&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; !empty( $tmp1[$id][$em_owner] ) &&&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; in_array( $em_adr,&nbsp; &nbsp;$tmp1[$id][$em_owner] ) &&&nbsp; &nbsp; &nbsp; &nbsp; in_array( $em_owner, $tmp1[$id][$em_adr] )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // indexes of pair values (both, direct and vice versa)&nbsp; &nbsp; &nbsp; &nbsp; $tm1 = array_search($em_adr,&nbsp; &nbsp;$tmp1[$id][$em_owner]);&nbsp; &nbsp; &nbsp; &nbsp; $tm2 = array_search($em_owner, $tmp1[$id][$em_adr]);&nbsp; &nbsp; &nbsp; &nbsp; // index of the row in main data set where these values a placed in defined positions.&nbsp; &nbsp; &nbsp; &nbsp; $in = getIndex($array, ['a'=>$em_owner, 'b'=>$em_adr, 'c'=>$id]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // removing data about paired values in $tmp1 array and in the main $array&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset( $tmp1[$id][$em_adr][$tm2] );&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset( $tmp1[$id][$em_owner][$tm1] );&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset( $array[$in] );&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}&nbsp;// additional function for receiving index of the rowfunction getIndex($data,$ne){&nbsp; &nbsp; foreach($data as $key=>$val){&nbsp; &nbsp; &nbsp; &nbsp; if ($val['id'] === $ne['c'] && $val['email_adr'] === $ne['a'] && $val['ad_owner_email'] === $ne['b']) return $key;&nbsp; &nbsp; }&nbsp; &nbsp; return -1;}演示2如果你有一个问题,那么你可以使用下一个代码而不是提到:SORT_REGULARarray_unique()$array = array_map("unserialize", array_unique(array_map("serialize", $array)));

神不在的星期二

这可能会对你的帮助。取原始数组,我们遍历所有元素并与 进行比较,如下所示:$arremail_adrad_owner_emailforeach($arr as $key => $value) {&nbsp; &nbsp; if(isset($arr[$key+1])) {&nbsp; &nbsp; &nbsp; &nbsp; if ($value['email_adr'] == $arr[$key + 1]['ad_owner_email']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($arr[$key]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}工作演示
打开App,查看更多内容
随时随地看视频慕课网APP