PHP Judgment Array Equality array operator Introduction _php tutorial

Source: Internet
Author: User
How can I tell if two arrays are equal? Actually very simple, with = = or = = = can be

The PHP manual describes the following,

Example name result
$a + $b Union of Joint $a and $b.
$a = = $b equal if the $a and $b have the same key/value pairs true.
$a = = = $b congruent if $a and $b have the same key/value pairs and the order and type are the same, TRUE.
$a! = $b is TRUE if the $a does not equal $b.
$a <> $b If the $a is not equal to $b true.
$a!== $b Not equal to TRUE if the $a is not all equals $b.
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 it, 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 methods of comparison around to judge. For example, use Array_diff ($a, $b) to compare the difference set of two arrays, which is equal if the difference set is null.

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.


[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 (Ten) "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 (Ten) "Strawberry"
["C"]=>
String (6) "Cherry"
}


http://www.bkjia.com/PHPjc/477181.html www.bkjia.com true http://www.bkjia.com/PHPjc/477181.html techarticle How can I tell if two arrays are equal? Actually very simple, with = = or = = = can be explained in the PHP manual as follows, example name results $a + $b joint $a and $b. $a = = $b ...

  • 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.