I have two arrays, $a and $b here, and need to check if they contain exactly the same elements (independently of the order). I am thinking of using
if (sizeof($a)==sizeof($b) AND array_diff($a,$b)==array())
{
}
But I am new to PHP, so I wonder: Is there a better way?
Since I need to use them as sets, maybe I should not use arrays at all but something else.
PHP array is an ordered map, so, it's a map that keeps the order. array elements just keep the order since they were added that's all.
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.
Check if two arrays are equal or not using Sorting Follow the steps below to solve the problem using this approach: Sort both the arrays. Then linearly compare elements of both the arrays. If all are equal then return true, else return false.
Use php function array_diff(array1, array2); It will return a the difference between arrays. If its empty then they're equal.
Well, we can do something like this:
if (count(array_diff(array_merge($a, $b), array_intersect($a, $b))) === 0) {
//they are the same!
}
The reason it works, is that array_merge
will make a big array that has all the elements of both $a
and $b
(all the elements that are in either $a
, $b
, or both). array_intersect
will create an array that has all the elements that are in both $a
and $b
only. So if they are different,, there must be at least one element that does not appear in both arrays...
Also note that sizeof
is not an actual function/construct, it's an alias. I'd suggest using count()
for clarity...
The accepted answer is was wrong. It will would fail on: https://3v4l.org/U8U5p
$a = ['x' => 1, 'y' => 2]; $b = ['x' => 1, 'y' => 1];
Here is a correct solution:
function consistsOfTheSameValues(array $a, array $b)
{
// check size of both arrays
if (count($a) !== count($b)) {
return false;
}
foreach ($b as $key => $bValue) {
// check that expected value exists in the array
if (!in_array($bValue, $a, true)) {
return false;
}
// check that expected value occurs the same amount of times in both arrays
if (count(array_keys($a, $bValue, true)) !== count(array_keys($b, $bValue, true))) {
return false;
}
}
return true;
}
Plus quite extensive unit tests: https://3v4l.org/m6lHv
<?php
// A unit testing framework in a tweet. https://gist.github.com/mathiasverraes/9046427
function it($m,$p){echo ($p?'✔︎':'✘')." It $m\n"; if(!$p){$GLOBALS['f']=1;}}function done(){if(@$GLOBALS['f'])die(1);}
function consistsOfTheSameValues(array $a, array $b)
{
// check size of both arrays
if (count($a) !== count($b)) {
return false;
}
foreach ($b as $key => $bValue) {
// check that expected value exists in the array
if (!in_array($bValue, $a, true)) {
return false;
}
// check that expected value occurs the same amount of times in both arrays
if (count(array_keys($a, $bValue, true)) !== count(array_keys($b, $bValue, true))) {
return false;
}
}
return true;
}
it('consist of the same values',
consistsOfTheSameValues([1], [1]) === true
);
it('consist of the same values',
consistsOfTheSameValues([1, 1], [1, 1]) === true
);
it('consist of the same values',
consistsOfTheSameValues(['1', 1], ['1', 1]) === true
);
it('consist of the same values',
consistsOfTheSameValues(['1', 1], [1, '1']) === true
);
it('consist of the same values',
consistsOfTheSameValues([1, '1'], ['1', 1]) === true
);
it('consist of the same values',
consistsOfTheSameValues([1, '1'], [1, '1']) === true
);
it('consist of the same values',
consistsOfTheSameValues(['x' => 1], ['x' => 1]) === true
);
it('consist of the same values',
consistsOfTheSameValues(['x' => 1], ['y' => 1]) === true
);
it('consist of the same values',
consistsOfTheSameValues(['y' => 1], ['x' => 1]) === true
);
it('consist of the same values',
consistsOfTheSameValues(['x' => 1, 'y' => 1], ['x' => 1, 'y' => 1]) === true
);
it('consist of the same values',
consistsOfTheSameValues(['y' => 1, 'x' => 1], ['x' => 1, 'y' => 1]) === true
);
it('consist of the same values',
consistsOfTheSameValues(['x' => 1, 'y' => 1], ['y' => 1, 'x' => 1]) === true
);
it('consist of the same values',
consistsOfTheSameValues(['y' => 1, 'x' => 1], ['y' => 1, 'x' => 1]) === true
);
it('consist of the same values',
consistsOfTheSameValues(['x' => 2, 'y' => 1], ['x' => 1, 'y' => 2]) === true
);
it('does not consist of the same values',
consistsOfTheSameValues([1], [2]) === false
);
it('does not consist of the same values',
consistsOfTheSameValues(['1'], [1]) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1], ['1']) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1], [1, 1]) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1, 1], [1]) === false
);
it('does not consist of the same values',
consistsOfTheSameValues(['1', 1], [1, 1]) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1, '1'], [1, 1]) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1, 1], ['1', 1]) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1, 1], [1, '1']) === false
);
it('does not consist of the same values',
consistsOfTheSameValues(['1', '1'], [1, 1]) === false
);
it('does not consist of the same values',
consistsOfTheSameValues(['1', '1'], ['1', 1]) === false
);
it('does not consist of the same values',
consistsOfTheSameValues(['1', '1'], [1, '1']) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1, 1], ['1', '1']) === false
);
it('does not consist of the same values',
consistsOfTheSameValues(['1', 1], ['1', '1']) === false
);
it('does not consist of the same values',
consistsOfTheSameValues([1, '1'], ['1', '1']) === false
);
it('does not consist of the same values',
consistsOfTheSameValues(['x' => 1], ['x' => 2]) === false
);
it('does not consist of the same values',
consistsOfTheSameValues(['x' => 1, 'y' => 1], ['x' => 1, 'y' => 2]) === false
);
it('does not consist of the same values',
consistsOfTheSameValues(['x' => 1, 'y' => 1], ['x' => 2, 'y' => 1]) === false
);
it('does not consist of the same values',
consistsOfTheSameValues(['x' => 2, 'y' => 1], ['x' => 1, 'y' => 1]) === false
);
@update:
Extensive unit test of @ircmaxell answer: https://3v4l.org/5ivgm
Extensive unit test of @Jon anwser: https://3v4l.org/CrTgQ
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With