Introduction to methods and array operators for determining equal arrays in php
This article mainly introduces the methods for determining the equality of arrays in php and the introduction of array operators. This article describes the relevant knowledge and provides the instance Code. For more information, see
How can we determine that two arrays are equal? In fact, it is very simple. You can use = OR =.
The php manual is described as follows:
Can the multi-dimensional arrays like array ('K' => array () be judged to be equal using the above method? Of course.
If the array is a digital index, pay attention to it. See the Code:
The Code is 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 to judge. For example, array_diff ($ a, $ B) is used to compare the difference sets of two arrays. If the difference set is an empty array, It is equal.
Then let's talk about the + plus operator of the array. + The difference between array_merge and array_merge is that when the key is equal, + is used, the left array overwrites the value of the right array. In contrast to array_merge, the following array overwrites the previous one.
The Code is 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 $
Echo "array_merge of \ $ B and \ $ a: \ n ";
Var_dump ($ c );
?>
Output after execution:
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 $:
Array (3 ){
["A"] =>
String (4) "pear"
["B"] =>
String (10) "strawberry"
["C"] =>
String (6) "cherry"
}