Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Comparing arrays?

    For example, I have an array:

    PHP Code:
    $user_answers = array(7,5,3,5,1,5,7,5,7,8); 
    And I have three more arrays.

    PHP Code:
    $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?

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    This is one way to do it:
    PHP Code:
    $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($results031); 
    $closest will contain an array of the keys that were the closest match to $sample

  • Users who have thanked Inigoesdr for this post:

    MHaris (08-20-2007)

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    How do I echo out the results?

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    print_r()

  • Users who have thanked Inigoesdr for this post:

    MHaris (08-20-2007)

  • #5
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    print_r()
    I mean the closest 3 candidates ids.

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by Inigoesdr View Post
    $closest will contain an array of the keys that were the closest match to $sample
    print_r($closest);

  • Users who have thanked Inigoesdr for this post:

    MHaris (08-20-2007)

  • #7
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    print_r($closest);
    PHP Code:
    <?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($results031);
    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:
    PHP Code:
    Array ( [0] => Array ( [0] => [1] => [2] => [3] => [4] => ) ) 
    $binned:
    PHP Code:
    Array ( [1] => Array ( [0] => [1] => [2] => [3] => [4] => ) [2] => Array ( [0] => [1] => [2] => [3] => [4] => ) [3] => Array ( [0] => [1] => [2] => [3] => [4] => ) [4] => Array ( [0] => [1] => [2] => [3] => [4] => ) [5] => Array ( [0] => [1] => [2] => [3] => [4] => ) [6] => Array ( [0] => [1] => [2] => [3] => [4] => ) ) 
    $results:
    PHP Code:
    Array ( [5] => [6] => [4] => [3] => [2] => [1] => 
    $closest:
    PHP Code:
    Array ( [5] => [6] => [4] => 
    According to the binned, the most closest should be:
    PHP Code:
    [2] => Array ( [0] => [1] => [2] => [3] => [4] => 
    but the closests vlaues in the $closests variable is not correct.

  • #8
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Help?

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Try this:
    PHP Code:
    $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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •