亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在PHP中將兩個數組元素相互匹配/配對并僅返回一個

如何在PHP中將兩個數組元素相互匹配/配對并僅返回一個

PHP
肥皂起泡泡 2022-08-19 15:47:19
是否有可能匹配所有并保留一個?例如,是匹配項,或將在篩選的數組中刪除。通過所有元素進行“交叉匹配”是可以的。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) "[email protected]"        ["ad_owner_email"]=>        string(23) "[email protected]"      }      [1]=>      array(3) {        ["id"]=>        string(5) "14598"        ["email_adr"]=>        string(23) "[email protected]"        ["ad_owner_email"]=>        string(14) "[email protected]"      }      [2]=>      array(3) {        ["id"]=>        string(5) "14598"        ["email_adr"]=>        string(23) "[email protected]"        ["ad_owner_email"]=>        string(21) "[email protected]"      }    }期望結果: array(2) {          [0]=>          array(3) {            ["id"]=>            string(5) "14598"            ["email_adr"]=>            string(23) "[email protected]"            ["ad_owner_email"]=>            string(14) "[email protected]"          }          [1]=>          array(3) {            ["id"]=>            string(5) "14598"            ["email_adr"]=>            string(23) "[email protected]"            ["ad_owner_email"]=>            string(21) "[email protected]"          }        }
查看完整描述

2 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

在第一步,您可以將所有可能的電子郵件對收集到數組中:$tmp


foreach($array as $record){

    $tmp1[$record['email_adr']][] = $record['ad_owner_email']; 

}

這將創建如下數組:


Array

(

    [[email protected]] => Array

        (

            [0] => [email protected]

        )


    [[email protected]] => Array

        (

            [0] => [email protected]

            [1] => [email protected]

        ) 

)

現在,您可以使用進一步的邏輯:


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)));


查看完整回答
反對 回復 2022-08-19
?
神不在的星期二

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]);

        }

    }

}

工作演示


查看完整回答
反對 回復 2022-08-19
  • 2 回答
  • 0 關注
  • 192 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號