Reputation: 1898
I have retrieved records of two users from a database in two arrays respectively. The records are of people who follow other people just like in twitter. Since two users may be following different number of people. So the length of the two arrays is different. i have created a new array that stores the common people ( people followed by both users). How can I get % similarity of the two users. Lets say if two users have 5 common followers they have more similarity than 2 users having 2 in common.
foreach($common as $row){
//do events
echo $row['name']."<br>";
$count_common++;
}
echo "total common ".$count_common;
$similarity = (count($common)/(count($user1_follows))*100);
The above formula does not calculate correct result since it is based for arrays having same length. Here is a related question to mine get the percentage of similarity of two arrays in php
Upvotes: 3
Views: 2376
Reputation: 57703
$p1 = array("foo", "bar", "grep");
$p2 = array("foo", "buzz", "fizz", "bar");
$similar = array_intersect($p1, $p2);
$p1_perc = count($similar) / count($p1); // 0.66..
$p2_perc = count($similar) / count($p2); // 0.5
To get one number:
$perc = 2 * count($similar) / (count($p1) + count($p2)); // 0.5714..
Upvotes: 10