我有两个键值对数组,一个是原始数组,另一个是需要删除的键值对数组。我需要删除一个特定的组合,即$ removeArray将包含:
Array([Word] => 78)
我试过了:
foreach($removeArray as $key => $value){unset($originalArray[$key][$value]);}
这根本不起作用。 我需要根据精确的键值对匹配进行删除。
修改
原始
Array ( [distribution] => 25 [watch] => 25 [electricity] => 25 [timepiece] => 8 [wristwatch] => 25 [energy] => 8 [transmission] => 8 [clock] => 16 )
删除
Array ( [timepiece] => 8 [energy] => 8 [watch] => 17 )
结果
Array ( [distribution] => 25 [watch] => 25 [electricity] => 25 [wristwatch] => 25 [transmission] => 8 [clock] => 16 )
注:
[watch] => 25
不受影响,因为它不等于[watch] => 17
答案 0 :(得分:5)
您可以使用array_diff_assoc()
来比较值和键:
$result = array_diff_assoc($original, $to_remove);
示例代码:
$removeArray = array(
'word'=>45,
'number'=>112,
'sign'=>2167
);
$originalArray = array(
'lorem'=>2343,
'ipsum'=>433,
'word'=>78,
'number'=>112,
'sign'=>2167
);
$result = array_diff_assoc($originalArray, $removeArray);
结果:
Array
(
[lorem] => 2343
[ipsum] => 433
[word] => 78
)
答案 1 :(得分:1)
试试这个
foreach($removeArray as $key => $value){
if($value==$originalArray[$key])
unset($originalArray[$key]);
}
答案 2 :(得分:0)
您可以查看:
if($originalArray[$key] == "78" && $key = "Word") {
unset($originalArray[$key]);
}