How can I tell if two arrays are equal? Actually very simple, with = = or = = = can be
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:
1<?PHP2 $a=Array("Apple", "banana");3 $b=Array(1 = "banana", "0" = "apple");4 5 Var_dump($a==$b);//bool (TRUE)6 Var_dump($a===$b);//bool (FALSE)7?>
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.
1<?PHP2 $a=Array("A" and "Apple", "b" = "banana");3 $b=Array("a" = "pear", "b" = "Strawberry", "C" and "Cherry");4 5 $c=$a+$b;//Union of $a and $b6 Echo"Union of \ $a and \ $b: \ n";7 Var_dump($c);8 9 $c=Array_merge($a,$b);//Union of $b and $aTen Echo"Array_merge of \ $b and \ $a: \ n"; One Var_dump($c); A?>
Post-Execution output:
Union of$aand$b:Array(3) { ["A"]=>string(5) "Apple" ["B"]=>string(6) "Banana" ["C"]=>string(6) "Cherry"}Array_mergeOf$band$a:Array(3) { ["A"]=>string(4) "Pear" ["B"]=>string(Ten) "Strawberry" ["C"]=>string(6) "Cherry"}
PHP determines whether two arrays are equal