2 回答
TA贡献1872条经验 获得超3个赞
在第一步,您可以将所有可能的电子邮件对收集到数组中:$tmp
foreach($array as $record){
$tmp1[$record['email_adr']][] = $record['ad_owner_email'];
}
这将创建如下数组:
Array
(
[jos@pl.nuu] => Array
(
[0] => boo@gmail.qom
)
[boo@gmail.qom] => Array
(
[0] => pelle@med.nuu
[1] => jos@pl.nuu
)
)
现在,您可以使用进一步的逻辑:
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]);
}
}
}
编辑根据以下要求
$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
)
)
您可以使用 next 逻辑而不是上一个逻辑:
// 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;
}
演示2
如果你有一个问题,那么你可以使用下一个代码而不是提到:SORT_REGULARarray_unique()
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
TA贡献1963条经验 获得超6个赞
这可能会对你的帮助。取原始数组,我们遍历所有元素并与 进行比较,如下所示:$arremail_adrad_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]);
}
}
}
工作演示
- 2 回答
- 0 关注
- 159 浏览
添加回答
举报