determine whether two arrays are equal? It’s actually very simple, just use == or ===
The description in the php manual is as follows:
Can multi-dimensional arrays like array('k'=>array()) be equal using the above method? Of course you can.
If the array is numerically indexed, you need to pay attention, see the code:
Copy code The code is as follows:
$a = array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
?>
In addition to the array operator ==, there are other more convoluted methods to judge. For example, use array_diff($a, $b) to compare the difference sets of two arrays. If the difference sets are empty arrays, they are equal.
Then let’s talk about the plus operator of arrays. The difference with array_merge is that when equal keys are encountered, when using , the left array will overwrite the value of the right array. On the contrary, with array_merge, the later array will overwrite the previous one.
Copy code The code is as follows:
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
$c = $a $b; // Union of $a and $b
echo "Union of $a and $b: n";
var_dump($c);
$c = array_merge($a, $b); // Union of $b and $a
echo "array_merge of $b and $a: n";
var_dump($c);
?>
Output after execution:
Copy code The code is as follows:
Union of $a and $b:
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(6) "cherry"
}
array_merge of $b and $a:
array(3) {
["a"]=>
string(4) "pear"
["b"]=>
string(10) "strawberry"
["c"]=>
string(6) "cherry"
}