Methods for judging array equality in PHP and introduction to array operators, array operators
How can I tell if two arrays are equal? Actually very simple, with = = or = = = can be
The PHP manual explains 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 numeric index, you should pay attention to the code:
Copy the 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)
?>
In addition to the array operator = =, there are other methods of comparison around to judge. For example, using Array_diff ($a, $b) to compare the difference set of two arrays, if the difference set is an empty array, then it will be equal.
And then say the array's + plus operator. + and Array_merge the difference when encountering equal key, with +, the left array will overwrite the value of the right array, Array_merge instead, the following array overrides the previous.
Copy the 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);
?>
Post-Execution output:
Copy the 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"
}
http://www.bkjia.com/PHPjc/976031.html www.bkjia.com true http://www.bkjia.com/PHPjc/976031.html techarticle The method of determining the equality of arrays in PHP and the introduction of array operators, how do array operators determine that two arrays are equal? Actually very simple, with = = or = = = can be explained in the PHP manual ...