[英]How to match/pair two array elements with each other and return only one in PHP
Is it possible to match all email_adr
and ad_owner_email
and keep one?是否可以匹配所有email_adr
和ad_owner_email
并保留一个? For example [0]
and [1]
is a match and [0]
or [1]
gets deleted in the filtered array.例如[0]
和[1]
是匹配项,并且[0]
或[1]
在过滤后的数组中被删除。 It's ok to get "cross matched" through all ["email_adr"]
and ["ad_owner_email"]
elements.可以通过所有["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"
}
}
This might help you on your way.这可能对您有所帮助。 Taking the original array, $arr
we loop through all elements and compare email_adr
to ad_owner_email
, as follows:取原始数组$arr
我们遍历所有元素并将email_adr
与ad_owner_email
进行比较,如下所示:
foreach($arr as $key => $value) {
if(isset($arr[$key+1])) {
if ($value['email_adr'] == $arr[$key + 1]['ad_owner_email']) {
unset($arr[$key]);
}
}
}
At the first step you can collect all possible email pairs into $tmp
array:第一步,您可以将所有可能的电子邮件对收集到$tmp
数组中:
foreach($array as $record){
$tmp1[$record['email_adr']][] = $record['ad_owner_email'];
}
This will create array like:这将创建数组,如:
Array
(
[jos@pl.nuu] => Array
(
[0] => boo@gmail.qom
)
[boo@gmail.qom] => Array
(
[0] => pelle@med.nuu
[1] => jos@pl.nuu
)
)
Now you can use further logic:现在您可以使用进一步的逻辑:
foreach($array as $ind=>$record){
if (isset($tmp1[$record['ad_owner_email']]) && !empty($tmp1[$record['ad_owner_email']]) )
{
if (in_array($record['email_adr'],$tmp1[$record['ad_owner_email']]))
{
// index of current value in $tmp1 array
$tm1 = array_search($record['ad_owner_email'],$tmp1[$record['email_adr']]);
// delete an element from $tmp1 and from $array
unset($tmp1[$record['email_adr']][$tm1]);
unset($array[$ind]);
}
}
}
EDIT According to the request below编辑根据下面的要求
$array = [["id"=>"111","email_adr"=>"AAA","ad_owner_email"=>"BBB"], ["id"=>"111","email_adr"=>"BBB","ad_owner_email"=>"CCC"], ["id"=>"111","email_adr"=>"BBB","ad_owner_email"=>"AAA"], <--discarded ["id"=>"500","email_adr"=>"QQQ","ad_owner_email"=>"PPP"], ["id"=>"500","email_adr"=>"PPP","ad_owner_email"=>"QQQ"], <--discarded ["id"=>"1000","email_adr"=>"PPP","ad_owner_email"=>"QQQ"], <-saved because id=1000 and differs from id=500 ]; --------==::Desired result::==----------- Array ( [0] => Array ( [id] => 111 [email_adr] => AAA [ad_owner_email] => BBB ) [1] => Array ( [id] => 111 [email_adr] => BBB [ad_owner_email] => CCC ) [2] => Array ( [id] => 500 [email_adr] => QQQ [ad_owner_email] => PPP ) [3] => Array ( [id] => 1000 [email_adr] => PPP [ad_owner_email] => QQQ ) )
you can use next logic instead of previous:您可以使用下一个逻辑而不是上一个:
// removes duplicate elements from multidimentional array
$array = array_unique($array, SORT_REGULAR);
// foreach($array ...) //<-- creating an array $tmp1
foreach($array as $ind=>$record){
$id = $record['id'];
$em_owner = $record['ad_owner_email'];
$em_adr = $record['email_adr'];
if (isset( $tmp1[$id][$em_owner] ) &&
!empty( $tmp1[$id][$em_owner] ) &&
in_array( $em_adr, $tmp1[$id][$em_owner] ) &&
in_array( $em_owner, $tmp1[$id][$em_adr] )
)
{
// indexes of pair values (both, direct and vice versa)
$tm1 = array_search($em_adr, $tmp1[$id][$em_owner]);
$tm2 = array_search($em_owner, $tmp1[$id][$em_adr]);
// index of the row in main data set where these values a placed in defined positions.
$in = getIndex($array, ['a'=>$em_owner, 'b'=>$em_adr, 'c'=>$id]);
// removing data about paired values in $tmp1 array and in the main $array
unset( $tmp1[$id][$em_adr][$tm2] );
unset( $tmp1[$id][$em_owner][$tm1] );
unset( $array[$in] );
}
}
// additional function for receiving index of the row
function getIndex($data,$ne){
foreach($data as $key=>$val){
if ($val['id'] === $ne['c'] && $val['email_adr'] === $ne['a'] && $val['ad_owner_email'] === $ne['b']) return $key;
}
return -1;
}
If you have a problem with SORT_REGULAR
then you can use next code instead of mentioned array_unique()
:如果您对SORT_REGULAR
有问题,那么您可以使用下一个代码而不是提到的array_unique()
:
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.