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

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

根據值php查找數組中的差異

根據值php查找數組中的差異

PHP
HUX布斯 2022-07-22 10:47:31
我有 2 個數組 -Array(    [0] => Array        (            [description] => 5390BF675E1464F32202B            [to_email] => [email protected]        )    [1] => Array        (            [description] => 5390BF675E1464F32202B            [to_email] => [email protected]        )    [2] => Array        (            [description] => 5390BF675E1464F32202B            [to_email] => [email protected]        ))Array(    [0] => Array        (            [to_email] => [email protected]        )    [1] => Array        (            [to_email] => [email protected]        ))我想從數組 1 中獲取與第二個數組不同的值。我試過使用 -$result = array_diff_assoc($array1, $array2);和$result = array_diff($array1, $array2);但是兩者都給出了錯誤,例如-注意:數組到字符串的轉換我期待的結果是Array( [0] => Array        (            [description] => 5390BF675E1464F32202B            [to_email] => [email protected]        ) )
查看完整描述

1 回答

?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

您可以使用 生成要排除的電子郵件地址列表array_column。我們使用 3 參數形式按電子郵件地址索引該數組,因為它更容易過濾:


$exclude_ids = array_column($array2, 'to_email', 'to_email');

然后我們可以使用array_filter過濾器$array1:


$output = array_filter($array1, function ($v) use ($exclude_ids) {

    return !isset($exclude_ids[$v['to_email']]);

});

print_r($output);

輸出:


Array

(

    [2] => Array

        (

            [description] => 5390BF675E1464F32202B

            [to_email] => [email protected]

        )    

)

3v4l.org 上的演示


請注意,如果您希望輸出數組重新索引為 0,只需使用


$output = array_values($output);


查看完整回答
反對 回復 2022-07-22
  • 1 回答
  • 0 關注
  • 130 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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