About array_ Diff (array1, array2) difference set to compare whether arrays are equal

Time:2021-2-16
Accidentally found that many friends like to use array_ Diff (array1, array2) to determine whether two arrays are equal,
I use it occasionally myself
But today, in the process of writing code, I found that it was not accurate.
First, let’s take a look at the definition of this function (PHP manual)
Compare array1 with one or more other numbers and return the values in array1 but not in other arrays.
To be honest, it’s a bit awkward to read. Some friends like to read the code directly,
Through the code in the manual, we found that comparing two arrays can find the difference set and indirectly judge the equality

<?php
$array1 = array(“a” => “green”, “red”, “blue”, “red”);
$array2 = array(“b” => “green”, “yellow”, “red”);
$result = array_diff($array1, $array2);
print_r($result);
?>

Here, as we expected, the returned result is array ([1] = > Blue)
A lot of friends see this and it’s over,
If the blue element is in $array2, what is the result of the comparison?
If you use it to judge whether two arrays are equal, I will tell you that the return value here is still empty!
Yes, this function only compares array1 with the following, and returns the difference set in array1,
The array in the back is not my own son. I don’t care about you at all!

Recommended Today

Java Engineer Interview Questions

The content covers: Java, mybatis, zookeeper, Dubbo, elasticsearch, memcached, redis, mysql, spring, spring boot, springcloud, rabbitmq, Kafka, Linux, etcMybatis interview questions1. What is mybatis?1. Mybatis is a semi ORM (object relational mapping) framework. It encapsulates JDBC internally. During development, you only need to pay attention to the SQL statement itself, and you don’t need to […]