Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two array and get all differences

I have two arrays like this.

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

So, the output should bring the difference in both arrays.

The output should be.

1,2,3,4,5 -> These numbers exist in both arrays, so these should be ignored.

7 and 6 -> These numbers are the un-common in both arrays, so I need these values in array.

The output should be 7 & 6.

Help me out. I have tried array_diff and other array elements.

like image 985
hjaffer2001 Avatar asked May 03 '13 09:05

hjaffer2001


People also ask

How do you find the difference between two arrays?

The array_diff() function compares the values of two (or more) arrays, and returns the differences. This function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in array2 or array3, etc.

How do I compare two arrays of arrays?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.

How do you compare an entire array?

How to Compare Array Contents? A simple way is to run a loop and compare elements one by one. Java provides a direct method Arrays. equals() to compare two arrays.

How do you compare two arrays to find matches?

JavaScript finding non-matching values in two arrays The code will look like this. const array1 = [1, 2, 3, 4, 5, 6]; const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const output = array2. filter(function (obj) { return array1. indexOf(obj) === -1; }); console.


1 Answers

try this

array_merge(array_diff($array1,$array2),array_diff($array2,$array1))
like image 72
Nauphal Avatar answered Sep 20 '22 02:09

Nauphal