Home >Backend Development >PHP Tutorial >How to determine array equality in PHP and introduction to array operators_PHP Tutorial
This article mainly introduces the method of judging array equality in php and the introduction of array operators. This article explains the relevant Knowledge and example code are given, friends in need can refer to it
How to determine if 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:
The code is as follows:
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.
The code is as follows:
$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:
The code is as follows: