| <?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" |
| |
| ?> |