我有 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);
- 1 回答
- 0 關注
- 130 瀏覽
添加回答
舉報
0/150
提交
取消