Home  >  Article  >  Backend Development  >  PHP determines array equality. Introduction to array operators_PHP tutorial

PHP determines array equality. Introduction to array operators_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:21:29819browse

How to determine if two arrays are equal? It’s actually very simple, just use == or ===

The php manual explains as follows,

Example Name Result
$a + $b union The union of $a and $b.
$a == $b equal TRUE if $a and $b have the same key/value pair.
$a === $b Congruent TRUE if $a and $b have the same key/value pairs and are of the same order and type.
$a != $b does not equal TRUE if $a does not equal $b.
$a <> $b does not equal TRUE if $a does not equal $b.
$a !== $b is not equal TRUE if $a is not equal to $b.
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


[php]
$a = array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");

var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
?>

$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 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 NULL, they are equal.

Then let’s talk about the + plus operator of arrays. The difference between + and array_merge is that when equal keys are encountered, when + is used, the left array will overwrite the value of the right array. On the contrary, with array_merge, the latter array will overwrite the previous one.


[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);
?>

$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:


[php]
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(10) "strawberry"
["c"]=>
string(6) "cherry"
}

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(10) "strawberry"
["c"]=>
string(6) "cherry"
}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477181.htmlTechArticleHow to determine whether two arrays are equal? In fact, it is very simple, just use == or ===. The PHP manual explains as follows, Example Name Result $a + $b is the union of $a and $b. $a == $b...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn