PHP checks if arrays are identical?

I am looking for a way to check if two arrays are identical, e.g.

  $a = array(
    '1' => 12,
    '3' => 14,
    '6' => 11
);
$b = array(
    '1' => 12,
    '3' => 14,
    '6' => 11
);

These two will be the same, but if one value has been changed, it will return false, I know that I can write a function, but is it already built?

+5
source share
2 answers

you can use

$a === $b // or $a == $b

usage example:

<?php
$a = array(
    '1' => 12,
    '3' => 14,
    '6' => 11
);
$b = array(
    '1' => 12,
    '3' => 14,
    '6' => 11
);
echo ($a === $b) ? 'they\'re same' : 'they\'re different';

echo "\n";
$b['1'] = 11;

echo ($a === $b) ? 'they\'re same' : 'they\'re different';

which will return

they're same
they're different

demo

+12
source

You can simply use $a == $bit if the order doesn't matter, or $a === $bif the order matters.

For instance:

$a = array(
    '1' => 12,
    '3' => 14,
    '6' => 11
);
$b = array(
    '1' => 12,
    '3' => 14,
    '6' => 11
);
$c = array(
    '3' => 14,
    '1' => 12,
    '6' => 11
);
$d = array(
    '1' => 11,
    '3' => 14,
    '6' => 11
);

$a == $b;   // evaluates to true
$a === $b;  // evaluates to true
$a == $c;   // evaluates to true
$a === $c;  // evaluates to false
$a == $d;   // evaluates to false
$a === $d;  // evaluates to false
+21
source

All Articles