phpcomparisonequals-operator

Should i use array_diff_assoc or ===?


I'm trying to compare two arrays to see if they are sorted or not. What is the difference between array_diff_assoc and comparing two arrays with the === operator? Are they the same thing?

For example

$arr_a
$arr_b

array_diff_assoc($arr_a, $arr_b)

Is the same as

$arr_a === $arr_b

?


Solution

  • Simple example tells us that these are different methods:

    $a = ['t' => 2, 'p' => 3];
    $b = ['p' => 3, 't' => 2];
    var_dump($a === $b); // false, arrays are not identical                
    var_dump(array_diff_assoc($a, $b));  
    // array(0) {} - means that there's no difference between these arrays
    // they have same keys with same values, but in different orders 
    // and for `===` order is important