1 $data=array('a'=>array('a'=> 2.5, 'b'=> 3.5, 'c'=> 3.0,'d'=> 3.5, 'e'=> 2.5, 'f'=> 3.0), 2 'b'=>array('a'=> 3.0, 'b'=> 3.5, 'c'=> 1.5, 'd'=> 5.0, 'f'=> 3.0, 'e'=> 3.5), 3 'c'=>array('a'=> 2.5, 'b'=> 3.0, 'd'=> 3.5,'f'=> 4.0), 4 'd'=>array('b'=> 3.5, 'c'=> 3.0, 'f'=> 4.5,'d'=> 4.0, 'e'=> 2.5), 5 'e'=>array('a'=> 3.0, 'b'=> 4.0, 'c'=> 2.0,'d'=> 3.0, 'f'=> 3.0, 'e'=> 2.0), 6 'f'=>array('a'=> 3.0, 'b'=> 4.0, 'f'=> 3.0,'d'=> 5.0, 'e'=> 3.5), 7 'g'=>array('b'=> 4.5,'e'=> 1.0,'d'=> 4.0), 8 'h'=>array('aa'=> 5.0 )); 9 10 function test($arr,$person,$anotherPerson){ 11 if(!array_key_exists($person, $arr) || !array_key_exists($anotherPerson, $arr)){ 12 return 0; 13 } 14 $mark = 0; 15 $result = 0; 16 foreach($arr[$person] as $key=>$value){ 17 if(array_key_exists($key,$arr[$anotherPerson])){ 18 $mark++; 19 $result += pow($value-$arr[$anotherPerson][$key],2); 20 } 21 } 22 if($mark == 0){ 23 return 0; 24 } 25 return 1/(1+sqrt($result)); 26 } 27 28 $result = test($data,'a','b'); 29 echo 'similarity is:'.$result;