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 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    For each value (Array within array)

    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 binned is Array => [1](candidate id) Array(answers)

    PHP Code:
    foreach($binned as $key => $value){
        
    $results[$key] = count(array_diff($user_answer$value));

    Here key is candidate id however value is Array and not the answers. How to get the value of the array within candidate id?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Since $binned is a 2-dimensional array, you need to dip into the 2nd dimension of the array with another foreach loop.
    PHP Code:
    foreach($binned as $key => $value) {
        foreach(
    $value as $answer) {
             echo 
    $answer//will display the answer column from your query
        
    }


  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    Since $binned is a 2-dimensional array, you need to dip into the 2nd dimension of the array with another foreach loop.
    PHP Code:
    foreach($binned as $key => $value) {
        foreach(
    $value as $answer) {
             echo 
    $answer//will display the answer column from your query
        
    }

    Oh, thanks.

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MHaris View Post
    Oh, thanks.
    PHP Code:
    foreach($binned as $key => $value){
            foreach (
    $value => $child_value){
                
    $results[$key] = count(array_diff($user_answer$child_value));
            }

    Error: Parse error: syntax error, unexpected T_DOUBLE_ARROW in C:\wamp\www\quiz\templates\compare_answers.php on line 12

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by MHaris View Post
    PHP Code:
    foreach($binned as $key => $value){
            foreach (
    $value => $child_value){
                
    $results[$key] = count(array_diff($user_answer$child_value));
            }

    Error: Parse error: syntax error, unexpected T_DOUBLE_ARROW in C:\wamp\www\quiz\templates\compare_answers.php on line 12
    The second foreach should say foreach ($value as $child_value).


  •  

    Posting Permissions

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