1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
 
class integer_comparison {
       
       //==================================================
       //=== STORE THE RECORDS TO COMPARE
       //==================================================
       private $records = array();
       
       //==================================================
       //=== INSERT A RECORD TO BE COMPARED
       //==================================================
       public function insert_record($key, Array $integers) {
              $this->records[$key] = $integers;
       }
       
       //==================================================
       //=== CALCULATE THE DIFFERENCE BETWEEN 2 INTEGERS - ALWAYS RETURN +INT
       //==================================================
       public static function get_difference($value1, $value2) {
              $difference = $value1 - $value2;
              if ($difference < 0) {
                     return $difference * -1;
              }
              return $difference;
       }
       
       //==================================================
       //=== ACTUALLY PROCESS THE RECORDS AGAINST A SINGLE RECORD & RETURN THE MATCHES IN ORDER
       //==================================================
       private function compare_records($compare_with_key) {
              
              //stores the running values processed throughout this function
              $record_totals = array();
              $total_score = 0;
              
              //loop through keys
              foreach ($this->records as $key=>$records) {
                     
                     //if this is the user who is being compared with, skip them
                     if ($compare_with_key == $key) {
                            continue;
                     }
                     
                     //define the storage element
                     $record_totals[$key] = 0;
                     
                     //process the records values
                     foreach ($records as $item=>$value) {
                            
                            //get the difference between the records rating and the rating that is being compaired with
                            $record_totals[$key] += self::get_difference($this->records[$compare_with_key][$item], $value);
                            
                     }
                     
              }
              
              //sort them in to order, the lower the number, the closer to the originals integers
              asort($record_totals);
              
              return $record_totals;
              
       }
       
       //==================================================
       //=== GET THE CLOSEST MATCHES TO THE COMPARED KEY
       //==================================================
       public function get_matches(
              $compare_with_key,                      //what key do you want to compare?
              $return_max_results = 5                     //total results returned, with the most similar first
              ) {
              
              //get the results of the comparison
              $raw_results = self::compare_records($compare_with_key);
              
              //whats the current iteration
              $counter = 0;
              $results = array();
              foreach ($raw_results as $key=>$value) {
                     
                     $counter++;
                     if ($counter > $return_max_results) {
                            break;
                     }
                     
                     $results[] = $key;
                     
              }
              
              return $results;
              
       }
       
       
}
 
//create the comparison class
$comparison = new integer_comparison();
 
//insert the records
$comparison->insert_record('Matt', array('X Factor' => 2, 'Hollyoaks' => 1, 'Eastenders' => 1, 'Manga Stuff' => 3));
$comparison->insert_record('Lauren', array('X Factor' => 5, 'Hollyoaks' => 4, 'Eastenders' => 4, 'Manga Stuff' => 3));
$comparison->insert_record('Anya', array('X Factor' => 3, 'Hollyoaks' => 1, 'Eastenders' => 3, 'Manga Stuff' => 5));
$comparison->insert_record('Lucia', array('X Factor' => 4, 'Hollyoaks' => 4, 'Eastenders' => 5, 'Manga Stuff' => 2));
$comparison->insert_record('Neil', array('X Factor' => 5, 'Hollyoaks' => 5, 'Eastenders' => 5, 'Manga Stuff' => 5));
$comparison->insert_record('Charlotte', array('X Factor' => 5, 'Hollyoaks' => 4, 'Eastenders' => 4, 'Manga Stuff' => 0));
$comparison->insert_record('Katy', array('X Factor' => 4, 'Hollyoaks' => 4, 'Eastenders' => 4, 'Manga Stuff' => 2));
$comparison->insert_record('Ashley', array('X Factor' => 4, 'Hollyoaks' => 2, 'Eastenders' => 3, 'Manga Stuff' => 0));
$comparison->insert_record('Vicky', array('X Factor' => 4, 'Hollyoaks' => 5, 'Eastenders' => 5, 'Manga Stuff' => 1));
 
//get the peoples ratings who most closely match the same as Matts ratings
$results = $comparison->get_matches('Matt', 3);
print 'Matt has similar tastes to (most similar first) ' . $results[0] . ', ' . $results[1] . ', and ' . $results[2];
 
//THE RESULT TO THE PRINTED STATEMENT ABOVE IS: "Matt has similar tastes to (most similar first) Anya, Ashley, and Katy"
 
?>