我有两个多维数组,我试图从第一个数组中的第二个数组中减去所有值。
第一个数组:名为$ arrayREQ2
Array (
[0] => Array (
[id] => 3
[subject] => asdasd
[from_id] => 3
[to_id] => 5
[time] => 2014-05-11 10:58:33
)
[1] => Array (
[id] => 2
[subject] => trolis
[from_id] => 4
[to_id] => 3
[time] => 2014-05-08 16:09:30
)
[2] => Array (
[id] => 1
[subject] => bandom
[from_id] => 3
[to_id] => 5
[time] => 2014-05-08 11:50:39
)
)
第二名:名为$arrayREQ1
Array (
[0] => Array (
[id] => 2
[subject] => trolis
[from_id] => 4
[to_id] => 3
[time] => 2014-05-08 16:09:30
)
)
我期待的结果:
Array (
[0] => Array (
[id] => 3
[subject] => asdasd
[from_id] => 3
[to_id] => 5
[time] => 2014-05-11 10:58:33
)
[1] => Array (
[id] => 1
[subject] => bandom
[from_id] => 3
[to_id] => 5
[time] => 2014-05-08 11:50:39
)
)
我尝试使用array_diff,但它不起作用:
$diffff = array_diff($arrayREQ2, $arrayREQ1);
它返回空数组。
答案 0 :(得分:0)
如果想要,你可以在这上面使用普通的'foreach'。他们检查是否匹配,如果是,则取消设置该特定密钥。考虑这个例子:
$arrayREQ1 = array(array('id' => 2, 'subject' => 'trollis', 'from_id' => 4, 'to_id' => 3, 'time' => '2014-05-08 16:09:30'));
$arrayREQ2 = array(
array('id' => 3, 'subject' => 'asdasd', 'from_id' => 3, 'to_id' => 5, 'time' => '2014-05-11 10:58:33'),
array('id' => 2, 'subject' => 'trollis', 'from_id' => 4, 'to_id' => 3, 'time' => '2014-05-08 16:09:30'),
array('id' => 1, 'subject' => 'bandom', 'from_id' => 3, 'to_id' => 5, 'time' => '2014-05-08 11:50:39'),
);
foreach($arrayREQ2 as $key => $value) {
if($arrayREQ1[0] === $value) {
unset($arrayREQ2[$key]);
}
}
print_r($arrayREQ2);
示例输出:
Array
(
[0] => Array
(
[id] => 3
[subject] => asdasd
[from_id] => 3
[to_id] => 5
[time] => 2014-05-11 10:58:33
)
[2] => Array
(
[id] => 1
[subject] => bandom
[from_id] => 3
[to_id] => 5
[time] => 2014-05-08 11:50:39
)
)