简体   繁体   中英

Finding the unique differences between two arrays of different length in PHP

Please, how do I compare the two arrays if they are equal disregarding duplicated numbers? Then output the unique compared values and the uniquely different values.

This is my array

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

my expected outputs:

compared values = 1,2,4,5,7,9
differences = 3,6
output = false

I have tried $a1 == $a2
I have also tried various ways, they don't just get it right.

For disregarding duplicated numbers, start by running each array through array_unique .

$a1 = array_unique($a1);
$a2 = array_unique($a2);

You can assign the unique arrays to new variables if you need to keep the original values.

Now, finding the elements in common is easy:

$compared_values = array_intersect($a1, $a2);

But finding the differences is a little tricky. array_diff returns values from its first argument that aren't present in the subsequent arguments. So it may return a different result depending on the order of arguments. (For example, the result of array_diff([1], [1, 2, 3]); will be an empty array.)

To be sure to get the full diff, you can merge the results of doing it both ways.

$differences = array_merge(array_diff($a1, $a2), array_diff($a2, $a1));

To see if the two arrays contained the same distinct set of values, you can just evaluate $differences as boolean. If it's empty, it will evaluate as false, and vice versa.

$output = !$differences;

Negating it like this will implicitly cast it to boolean, so it will be false if there were any differences and true if it's empty.

The way you tried, $a1 == $a2 can't work because array equality in PHP means the two arrays have the same key/value pairs. Duplicated values and different orders (hence different keys) will prevent them from being equal.

Ok then... I'll at least write something leveraging the plethora of PHP array functions...

<?php

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

$diff = array_diff($a2, $a1);

sort($a1);

echo 'compared values = ' . implode(",", array_unique($a1)) . '<br>'; 
echo 'differences = ' . implode(",", array_unique($diff)) . '<br>';
//?
$output = (empty($diff)) ? 'true' : 'false';
echo 'output = ' . $output;

compared values = 1,2,4,5,7,9

differences = 3,6

output = false

It is pretty ugly code, much like I made a proof of concept for you, I'll let you optimise to suit your needs. This will give you your expected results. I hope it helps. * I just assumed that output = false when the arrays aren't identical. Feel free to tell me if i'm wrong.

//Array 1
$a1 = array(1, 2, 9, 2, 4, 5, 5, 7); 
//Array 2
$a2 = array(1, 2, 4, 3, 3, 5, 6, 7, 9, 2 );

//To get your example results, they need to be sorted and the duplicates removed.
$a1 = array_unique($a1);
$a2 = array_unique($a2);
sort($a1);
sort($a2);
//Get smaller array as array1
$array1 = (sizeof($a1) > sizeof($a2)) ? $a1:$a2;
//Get bigger array as array2
$array2 = (sizeof($a1) < sizeof($a2)) ? $a1:$a2;
//Check the difference between each.
$diff = array_diff($array1, $array2);


echo "Compared values = " . implode(',', array_intersect($a1, $a2)).PHP_EOL;
echo "differences = " . implode(',', $diff).PHP_EOL;
echo "output = " . (($diff) ? "false" : "true");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM