获取php中两个数组的相似度百分比

jer*_*ass 2 php

我需要采用两个数组并提出相似度的百分比.即:

array( 0=>'1' , 1=>'2' , 2=>'6' , 3=>array(0=>1))
Run Code Online (Sandbox Code Playgroud)

VERS

array( 0=>'1' , 1=>'45' , 2=>'6' , 3=>array(0=>1))
Run Code Online (Sandbox Code Playgroud)

在哪里我会认为%是75

要么

array( 0=>'1' , 1=>'2' , 2=>'6' , 3=>array(0=>'1'))
Run Code Online (Sandbox Code Playgroud)

VERS

array( 0=>'1' , 1=>'2' , 2=>'6' , 3=>array(0=>'55'))
Run Code Online (Sandbox Code Playgroud)

不知道如何处理这个...只需要以可行的浮动百分比结束.谢谢 .

小智 6

以下是我最近解决这个问题的方法:

$array1 = array('item1','item2','item3','item4','item5');
$array2 = array('item1','item4','item6','item7','item8','item9','item10');

// returns array containing only items that appear in both arrays
$matches = array_intersect($array1,$array2);

// calculate 'similarity' of array 2 to array 1
// if you want to calculate the inverse, the 'similarity' of array 1
// to array 2, replace $array1 with $array2 below

$a = round(count($matches));

$b = count($array1);

$similarity = $a/$b*100;

echo 'SIMILARITY: ' . $similarity . '%';

// i.e., SIMILARITY: 40%
// (2 of 5 items in array1 have matches in array2 = 40%)
Run Code Online (Sandbox Code Playgroud)