-3

我有两个数组

$a1     = array('1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5', '6'=>'6');
$a2     = array('1'=>'1', '2'=>'2', '3'=>'3', '6'=>'6', '7'=>'7', '8'=>'8');

我想得到两个数组作为输出第一个数组,它返回那些从第一个数组中删除的元素。返回新元素的第二个数组

4

2 回答 2

1

要获得一个包含不再在第二个数组中的所有元素的数组,可以使用array_diff

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

Array
(
    [1] => blue
)

要获取包含两个数组中的所有元素(未删除)的数组,您可以使用array_intersect

<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>

Array
(
    [a] => green
    [0] => red
)
于 2013-05-07T07:18:24.950 回答
0
$a1   = array('1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5', '6'=>'6');
$a2   = array('1'=>'1', '2'=>'2', '3'=>'3', '6'=>'6', '7'=>'7', '8'=>'8');
$out1 = array_diff($a1,$a2);
$out2 = array_diff($a2,$a1);
于 2013-05-07T07:20:56.467 回答