1, 'b' => 2, 'c'"/> 1, 'b' => 2, 'c'">

Home >Backend Development >PHP Problem >Are php arrays equal?

Are php arrays equal?

PHPz
PHPzOriginal
2023-05-22 22:25:06527browse

In PHP, you can use the "==" and "===" operators to compare equality between arrays. Below are explanations and examples of both operators.

The "==" operator

The "==" operator is used to compare whether two arrays have the same key-value pairs, regardless of the order of the keys. Two arrays are considered equal if every key in the two arrays exists in the other array and the corresponding values ​​are equal.

For example, consider the following code:

$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('b' => 2, 'a' => 1, 'c' => 3);

if ($array1 == $array2) {
    echo "两个数组相等";
} else {
    echo "两个数组不相等";
}

In this example, the keys and values ​​in $array1 and $array2 are the same, just in a different order. So running this code will output "Both arrays are equal".

The "===" operator

The "===" operator is used to compare whether two arrays are exactly equal in terms of keys and values, including the order of the keys. This means that both arrays must have the same keys and corresponding values, and these keys and values ​​must appear in the same order.

For example, consider the following code:

$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('b' => 2, 'a' => 1, 'c' => 3);

if ($array1 === $array2) {
    echo "两个数组相等";
} else {
    echo "两个数组不相等";
}

In this example, the keys and values ​​in $array1 and $array2 are the same, but in a different order. Therefore, running this code will output "the two arrays are not equal".

Value Type

Note that arrays in PHP can contain different types of values, including strings, integers, floating point numbers, Boolean values, objects, and other arrays. The types of these values ​​are also taken into account when comparing arrays using the "==" and "===" operators.

For example, consider the following code:

$array1 = array('a' => '1', 'b' => 2, 'c' => 3);
$array2 = array('a' => 1, 'b' => '2', 'c' => 3);

if ($array1 == $array2) {
    echo "两个数组相等";
} else {
    echo "两个数组不相等";
}

In this example, the keys and values ​​in $array1 and $array2 are the same, but the value of $a is in an array of characters Strings, and integers in another array. Therefore, running this code will output "the two arrays are not equal".

Summary

In PHP, use the "==" and "===" operators to compare two arrays for equality. However, the order and type of keys and values ​​must be taken into account when comparing.

The above is the detailed content of Are php arrays equal?. For more information, please follow other related articles on the PHP Chinese website!

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