Determine if 2 partner arrays have the same content

I am trying to have a PHP function that compares array 2 and returns true if they are identical.

Example:

assertArrayEquals(array('a'=>1, 'b'=>2), array('a'=>1, 'b'=>2)); // True assertArrayEquals(array('a'=>1, 'b'=>2), array('b'=>2, 'a'=>1)); // True assertArrayEquals(array('a'=>1, 'b'=>2), array(1, 2)); // false assertArrayEquals(array(2, 1), array(1, 2)); // false 

Any idea?

Edit: Other test examples:

 assertArrayEquals(array('a'=>1, 'b'=>2), array('a'=>1, 'b'=>2, 'c'=>3)); // false assertArrayEquals(array('a'=>1, 'b'=>2, 'c'=>3), array('a'=>1, 'b'=>2)); // false assertArrayEquals(array('a'=>0), array('a'=>'foo')); // false 
+4
source share
6 answers

Use array_diff_assoc :

 <?php var_dump(assertArrayEquals(array('a'=>1, 'b'=>2), array('a'=>1, 'b'=>2))); // True var_dump(assertArrayEquals(array('a'=>1, 'b'=>2), array('b'=>2, 'a'=>1))); // True var_dump(assertArrayEquals(array('a'=>1, 'b'=>2), array(1, 2))); // false var_dump(assertArrayEquals(array(2, 1), array(1, 2))); // false function assertArrayEquals($a, $b){ return count(array_diff_assoc($a,$b)) == 0 && count(array_diff_assoc($b,$a)) == 0; } 

http://codepad.org/YoZIGjBY

+3
source

browse http://php.net/array_diff and http://php.net/array_intersect

Or if all you do is check for equality:

sort both arrays and check with === (or == if you are a scary kitten)

+2
source

Use == .

php Manual: Array operators :

$a == $b - Equality - TRUE if $ a and $ b have the same key / value pairs.


Example 4 will fail because the key / value pairs do not match:

0 => 2, 1 => 1 vs 0 => 1, 1 => 2 .


EDIT:

If arrays contain different types of variables, use

 function assertArrayEquals($a, $b) { asort($a); asort($b); return $a === $b; } 

This will give the expected results even for an example in a comment:

 echo (int)assertArrayEquals(array('a'=>1, 'b'=>2), array('a'=>1, 'b'=>2)); // true echo (int)assertArrayEquals(array('a'=>1, 'b'=>2), array('b'=>2, 'a'=>1)); // true echo (int)assertArrayEquals(array('a'=>1, 'b'=>2), array(1, 2)); // false echo (int)assertArrayEquals(array(2, 1), array(1, 2)); // false echo (int)assertArrayEquals(array('a'=>0,'b'=>1), array('a'=>'cake','b'=>1)); // false echo (int)assertArrayEquals(array('a'=>'0','b'=>1), array('a'=>'cake','b'=>1)); // false 
+1
source

It looks like you want to make sure the arrays have the same key / value pairs (strongly typed), but not necessarily in the same order. So, make the orders consistent and strictly compare them.

 function assertArrayEquals($a, $b) { ksort($a); ksort($b); return ($a === $b); } 

As a result of testing

 testAssertArrayEquals(array('a'=>1, 'b'=>2), array('a'=>1, 'b'=>2), true); testAssertArrayEquals(array('a'=>1, 'b'=>2), array('b'=>2, 'a'=>1), true); testAssertArrayEquals(array('a'=>1, 'b'=>2), array(1, 2), false); testAssertArrayEquals(array(2, 1), array(1, 2), false); testAssertArrayEquals(array('a'=>1, 'b'=>2), array('a'=>1, 'b'=>2, 'c'=>3), false); testAssertArrayEquals(array('a'=>1, 'b'=>2, 'c'=>3), array('a'=>1, 'b'=>2), false); testAssertArrayEquals(array('a'=>0,'b'=>1), array('a'=>'cake','b'=>1), false); // Extra function testAssertArrayEquals($a, $b, $expected) { $result = assertArrayEquals($a, $b); echo "Got " . var_export($result,1) . " expected " . var_export($expected,1) . PHP_EOL; } // Got true expected true // Got true expected true // Got false expected false // Got false expected false // Got false expected false // Got false expected false // Got false expected false 

I would also suggest choosing the best function name; your current choice will not bring you any benefits in the long run!

+1
source
 function assertArrayEquals($array1, $array2) { return !array_diff($array1, $array2) and !array_diff($array2, $array1); } 

We mainly use array_diff () to check if there is any difference between the two given arrays. If they are not, then they are equal.

Despite this clean form, you can use simple == to compare two arrays. Here are some tested samples:

 echo (array('a'=>1, 'b'=>2) == array('a'=>1, 'b'=>2)) ? 'true' : 'false'; // true echo (array('a'=>1, 'b'=>2) == array('b'=>2, 'a'=>1)) ? 'true' : 'false'; // true echo (array('a'=>1, 'b'=>2) == array(1, 2)) ? 'true' : 'false'; // false echo (array(2, 1) == array(1, 2)) ? 'true' : 'false'; // false 

This script output is: truetruefalsefalse ;

0
source

PHP has a function for this.

 $result = array_diff($array1, $array2); 
Result

$ returns everything that differs from arrays. If they are the same, they will not return anything.

0
source

Source: https://habr.com/ru/post/1346795/


All Articles