Introduction to php's array equality array Operators

Source: Internet
Author: User

How can we determine that two arrays are equal? In fact, it is very simple. You can use = OR =.

The php manual describes the following,

Example name result
$ A + $ B join $ a and $ B.
$ A = $ B is equal. If $ a and $ B have the same key/value pair, the value is TRUE.
$ A ===$ B. TRUE if $ a and $ B have the same key/value pairs and the order and type are the same.
$! = $ B: TRUE if $ a is not equal to $ B.
$ A <> $ B: TRUE if $ a is not equal to $ B.
$! = $ B incomplete. If $ a is not all equal to $ B, TRUE is returned.
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.


[Php]
<? Php
$ A = array ("apple", "banana ");
$ B = array (1 => "banana", "0" => "apple ");
 
Var_dump ($ a = $ B); // bool (true)
Var_dump ($ a ===$ B); // bool (false)
?>

<? 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 NULL, 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.


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

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


[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 $:
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 $:
Array (3 ){
["A"] =>
String (4) "pear"
["B"] =>
String (10) "strawberry"
["C"] =>
String (6) "cherry"
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.