How do you tell two arrays to be equal? Actually very simple, with = = or = = on it
The PHP manual describes the following:
Can a multidimensional array such as Array (' K ' =>array ()) be judged equal by the above method? Of course I can.
If the array is a digital index, you should pay attention to it, see Code:
Copy Code code as follows:
<?php
$a = Array ("Apple", "banana");
$b = Array (1 => "banana", "0" => "apple");
Var_dump ($a = = $b); BOOL (TRUE)
Var_dump ($a = = = $b); BOOL (FALSE)
?>
Besides the array operators, there are other methods to judge. For example, using Array_diff ($a, $b) to compare the difference sets of two arrays, if the difference set is an empty array, it is equal.
And then again, the + plus operator for the array. The difference between + and array_merge when you encounter an equal key, when you use +, the left array overrides the value of the right array, Array_merge instead, the rear array overwrites the front.
Copy Code code as follows:
<?php
$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 code 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 (Ten) "Strawberry"
["C"]=>
String (6) "Cherry"
}