Search code examples
phprecommendation-engine

Finding Similarity between two arrays


I want to build a Recommendation system that recommends people base on their preferences towards user details for example if User1 wants someone who is male and lives america, an array would be made of this being User1{male, america} while the second array would be the other users details for example user2 being female and from america her array would be User2{female, america}, user 3 being {male, america} I want to be able to find the similarity distance between user 1 and the other users array and base on the score it would be listed from the highest to the lowest.


Solution

  • The dirty way of doing it:

    foreach($item in $array_one){
        foreach($item_two in $array_two){
             if($item == $item_two){
                 echo "Euston, we found a match!";
             }
        }
    }
    

    The clean way of doing it:

    $intersection = array_intersect($array_one, $arrary_two);
    if (in_array($value_to_look_for, $intersection)) {
        echo "Euston, we have a match!";
    }