I need to take two arrays and come up with a percent of similarity. ie:
array( 0=>'1' , 1=>'2' , 2=>'6' , 3=>array(0=>1))
vers
array( 0=>'1' , 1=>'45' , 2=>'6' , 3=>array(0=>1))
Where I would think that the % is 75
or
array( 0=>'1' , 1=>'2' , 2=>'6' , 3=>array(0=>'1'))
vers
array( 0=>'1' , 1=>'2' , 2=>'6' , 3=>array(0=>'55'))
Not sure how to approach this.. just need to end up with a workable float percent. Thank you .
Here's how I tackled this problem recently:
$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%)
Assuming both arrays are the same length, you can iterate through and see which values are the same for the keys, for example:
<?php
$a = array(1,2,3,4);
$b = array(1,2,4,4);
$c = 0;
foreach ($a as $k=>$v) {
if ($v == $b[$k]) $c++;
}
echo ($c/count($a))*100;
// outputs 75
?>
Or just checking whether they contain similar items using in_array.
<?php
$a = array(1,2,3);
$b = array(1,2,4);
$c = 0;
foreach ($a as $i) {
if (in_array($i,$b)) $c++;
}
echo ($c/count($a))*100;
// outputs 66.66...
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With