A
A
Arystan Kaliakparov2017-09-03 11:48:37
PHP
Arystan Kaliakparov, 2017-09-03 11:48:37

How to compare two arrays percentage similarity?

Let's say I have two arrays

$arr[0]= "a";
    $arr[1]= "q";
    $arr[2]= "w";
    $arr[3]= "e";
    $arr[4]= "r";
    
    $arr2[0]= "1";
    $arr2[1]= "a";
    $arr2[2]= "12";
    $arr2[3]= "q";

I need to determine the percentage of their convergence
First, I tried using the array_intersect function
array_intersect($arr, $arr2);
, I now have an array
Array ( [0] => a [1] => q )
from this we learn that:
40%($arr) = 50%($ arr2)
Now I don't know what to do next, how to find out how much percentage these two arrays are similar

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fetur, 2017-09-03
@Fetur

You can do so.

$arr1 = [1,2,3,4,5,6,7,8,9,10];
$arr2 = [2,3];

$arr3 = array_diff($arr1, $arr2); 
$undiff_count = count($arr1) - count($arr3); //Нужно число выкинутых, а не оставшихся
$similiar_count = $undiff_count * 100 / count($arr1);
echo 'Схожесть массивов ' . $similiar_count .'%';

P
Prince of Denmark, 2017-09-03
@pezdatskiy

The similarity of two arrays is the arithmetic mean. (A + B) / 2.
You have already deduced the similarity of one with the other.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question