0

I have two arrays:

$array1 = array(1,2,3,4);
$array2 = array(1,2,3,4,5,6,7);

How can I remove the matched values from the arrays and display the remaining values?

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • possible duplicate of [How to compare two arrays and remove matching elements from one for the next loop?](http://stackoverflow.com/questions/225371/how-to-compare-two-arrays-and-remove-matching-elements-from-one-for-the-next-loo?rq=1) – Wesley Murch Mar 25 '14 at 06:42
  • Does this answer your question? [How to compare two arrays and remove matching elements from one for the next loop?](https://stackoverflow.com/questions/225371/how-to-compare-two-arrays-and-remove-matching-elements-from-one-for-the-next-loo) – Rob Apr 24 '20 at 14:27

2 Answers2

0

You should use array_diff

<?php
$array1 =array(1,2,3,4);
$array2 = array(1,2,3,4,5,6,7);
print_r(array_values(array_diff($array2,$array1)));

OUTPUT :

Array
(
    [0] => 5
    [1] => 6
    [2] => 7
)
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

Try this:

You can use array_diif() function to get those values.

$A = array(1,2,3,4,5,6,7);
$B = array(1,2,3,4);

$C = array_intersect($A,$B);  //equals (1,2,3,4)
$D = array_diff($A,$B);       //equals (5,6,7)
Anand Solanki
  • 3,419
  • 4
  • 16
  • 27