...

View Full Version : Comparing arrays?



MHaris
08-17-2007, 07:30 PM
For example, I have an array:


$user_answers = array(7,5,3,5,1,5,7,5,7,8);

And I have three more arrays.


$obama_answers = array(7,8,5,3,4,6,7,8,5,4);
$bush_answers = array(7,5,3,5,1,5,8,5,7,8);
$clinton_answers = array(7,5,4,5,1,6,7,5,7,9);
....

I want to compare the $user_answer with those three arrays and echo out the most closest 3 arrays in the nearest order.

Please keep in mind, I'll have more than 3 arrays to compare with.

How would I achieve this?

Inigoesdr
08-17-2007, 07:52 PM
This is one way to do it:

$sample = array(7,5,3,5,1,5,7,5,7,8);

$tests = array (
'obama_answers' => array(7,8,5,3,4,6,7,8,5,4),
'bush_answers' => array(7,5,3,5,1,5,8,5,7,8),
'clinton_answers' => array(7,5,4,5,1,6,7,5,7,9),
'some_answers' => array(1,2,3,4,5,6,7,8,9,0),
'other_answers' => array(1,2,3,4,5,6,7,8,9,0)
);

$results = array();
foreach($tests as $test => $test_array)
{
$results[$test] = count(array_diff($sample, $test_array));
}
asort($results);
$closest = array_slice($results, 0, 3, 1);

$closest will contain an array of the keys that were the closest match to $sample

MHaris
08-18-2007, 01:01 AM
How do I echo out the results?

Inigoesdr
08-18-2007, 01:11 AM
print_r()

MHaris
08-18-2007, 01:12 AM
print_r()

I mean the closest 3 candidates ids.

Inigoesdr
08-18-2007, 01:21 AM
$closest will contain an array of the keys that were the closest match to $sample

print_r($closest);

MHaris
08-20-2007, 06:20 AM
print_r($closest);


<?php

$result = mysql_query("SELECT * FROM answers") or die(mysql_error());
while ($row = mysql_fetch_assoc($result)){
$cID = $row['candidate_id'];
$binned_key = $cID;
$binned[$binned_key][] = $row["answer"];
}

$results = array();
foreach($binned as $key => $value){
$results[$key] = count(array_diff($user_answer, $value));
}

asort($results);
$closest = array_slice($results, 0, 3, 1);
foreach($closest as $key => $value){
$result = mysql_query("SELECT * FROM candidates WHERE id='$key'") or die(mysql_error());
while ($row = mysql_fetch_array($result)){
echo $row['name']."<br/>";
}
}

?>

The closest array is not really the closest array? Is there a problem with the code?

$user_answer:

Array ( [0] => Array ( [0] => 2 [1] => 2 [2] => 2 [3] => 2 [4] => 2 ) )

$binned:

Array ( [1] => Array ( [0] => 2 [1] => 1 [2] => 1 [3] => 1 [4] => 2 ) [2] => Array ( [0] => 2 [1] => 2 [2] => 2 [3] => 2 [4] => 2 ) [3] => Array ( [0] => 1 [1] => 2 [2] => 2 [3] => 2 [4] => 1 ) [4] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 2 ) [5] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 ) [6] => Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 1 ) )

$results:

Array ( [5] => 1 [6] => 1 [4] => 1 [3] => 1 [2] => 1 [1] => 1 )

$closest:

Array ( [5] => 1 [6] => 1 [4] => 1 )

According to the binned, the most closest should be:

[2] => Array ( [0] => 2 [1] => 2 [2] => 2 [3] => 2 [4] => 2 ) but the closests vlaues in the $closests variable is not correct.

MHaris
08-20-2007, 04:34 PM
Help?

Inigoesdr
08-21-2007, 12:06 AM
Try this:

$results[$key] = count(array_diff_assoc($user_answer[0], $value));
If it doesn't work then dump the two MySQL tables so I can test the full code in action. Also, if the index of $user_answer changes you'll have to compensate.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum