homePageCreator homePageCreator - 6 months ago 38
PHP Question

Compare multidimensional arrays in PHP

How can I compare multidimensional arrays in php? Is there a simple way?

Answer

The simplest way I know:

$a == $b;

Note that you can also use the ===. The difference between them is:

  1. With ===, order is important:

    $a = array(0 => 'a', 1 => 'b');
    $b = array(1 => 'b', 0 => 'a');
    var_dump($a == $b);  // true
    var_dump($a === $b); // false
    
  2. With ===, types matter:

    $a = array(0, 1);
    $b = array('0', '1');
    var_dump($a == $b);  // true
    var_dump($a === $b); // false
    

Reference: Array operators