How to judge that different arrays of the same elements in different order are the same?

for example, according to the passed array, if the elements in the array are the same (the order may be different), convert to a string as the key of redis

$a=["a","b","c"];
$b=["b","a","c"];
$c=["c","b","a"];
$d=["c","b","a","d"];
function test($arr){
   // do sth
   $tmp = doSth($arr);
   $redis->set(implode(",",$tmp),time());
}
function doSth($arr){
    sort($arr);//
    return $arr;
}
//$a,$b,$c a b c  key  doSth($a) doSth($b) doSth($c) $d
Jun.20,2021

learn about these two methods and make rational use of them.

PHP: array_intersect-Manual- http://php.net/manual/zh/func.
PHP: array_diff-Manual- http://php.net/manual/zh/func.

in fact, someone upstairs has already answered to the point. Using array_intersect to get intersection,
is actually a question of similarity calculation. Yesterday I wrote an article about using article Tag to recommend similar articles , using this method,
jaccard similarity

given that the Jaccard coefficients of two sets A blockquote Bjaccard are defined as the ratio of the size of the intersection of An and B to the size of the union of An and B, it is defined as follows: < / Jaccard >

bVbgVBi?w=252&h=38

as long as the similarity is 1, it proves that the elements in the two arrays are exactly the same

.

create a collection set iterate through each array, add to the set collection, and you get the result

  

if the resources are sufficient, they can be sorted and compared in turn.

Menu