1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

[PHP] match multi dimensional array with associative array

Discussion in 'PHP' started by gary4gar, Jul 29, 2010.

  1. #1
    Hi,
    I am trying to save response out of HTML form which has about 100questions. In question user has to select 1 option, out of three.Now the Objective is to save the responses of the user, so we admin can view them later.I am more interested in their actual response rather than values field. my HTML form goes something like this:
    PHP:
    1. <form method="post"  action="save.php">
    2.  
    3. <table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
    4.   <tr>
    5.     <td colspan="3">&nbsp;</td>
    6.   </tr>
    7.   <tr>
    8.     <td width="160" valign="top">
    9.  
    10. <p>
    11. <strong>Select the most appropriate option</strong>
    12.  
    13. <table border="0">
    14. <tr><td rowspan="4">
    15.  
    16. <table border="0">
    17. <tr><td><input type="hidden" name="question-15-order" value="0" /><input type="radio" name="question-15-answer" value="1" /></td><td>I like new ideas when they have practical applications.</td></tr>
    18. <tr><td><input type="radio" name="question-15-answer" value="2" /></td><td>I fall in love with new ideas and concepts for their own sake.</td></tr>
    19. <tr><td><input type="radio" name="question-15-answer" value="3" /></td><td>I'm really in between.</td></tr>
    20. </table>
    21.  
    22. <br />
    23.  
    24. <table border="0">
    25. <tr><td><input type="hidden" name="question-11-order" value="0" /><input type="radio" name="question-11-answer" value="1" /></td><td>I am a mixer and mingler at parties.</td></tr>
    26. <tr><td><input type="radio" name="question-11-answer" value="2" /></td><td>I prefer one-on-one conversations.</td></tr>
    27. <tr><td><input type="radio" name="question-11-answer" value="3" /></td><td>I'm really in between.</td></tr>
    28. </table>
    29. <br />
    30.  
    31. <snip>
    32. ......and so on
    33.  
    Now,to keep track of the actual answers I created an array which hold all the questions and their 3 respective options:

    PHP:
    1. $ questions = Array(
    2.     [question-15-answer] => Array
    3.         (
    4.             [1] => I like new ideas when they have practical applications.
    5.             [2] => I fall in love with new ideas and concepts for their own sake.
    6.             [3] => I am really in between.
    7.         )
    8.  
    9.     [question-11-answer] => Array
    10.         (
    11.             [1] => I am a mixer and mingle at parties.
    12.             [2] => I prefer one-on-one conversations.
    13.             [3] => I am really in between
    14.         )
    15.  
    16. <snip>
    17. ......and so on
    18.  
    also, there $_POST array

    PHP:
    1. (
    2.     [question-15-order] => 0
    3.     [question-15-answer] => 3
    4.     [question-11-order] => 0
    5.     [question-11-answer] => 3
    6. <snip>
    7. ......and so on
    8.  
    Now, what I want is to match $_POST array's [question-15-answer] value with $questions array's [question-15-answer] value and store that somewhere e.g database table. so admin that view them later. Also, need to ignore the order field on $_POST array but that's not important.

    I need something like this but not sure How do I loop over this:
    PHP:
    1. $selected = $_POST[$value];
    2. $choice = $questions[$key][$selected];
    3. echo $choice;
    Hoping to find some php coders, so you can guide me
    gary4gar, Jul 29, 2010 IP
  2. Zeh.

    Zeh. Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Myabe:
    PHP:
    1.  
    2. <?php
    3. $questionsCount = count($questions);
    4. for ($i = 0; $i < $questionsCount; $i++) {
    5.     $selected = $_POST['question-'.$i.'-answer'];
    6.     $choice = $questions[$i][$selected];
    7.     echo 'Answer '.$i.': '.$choice . '<br />';
    8. }
    9. ?>
    10.  
    ?

    Regards.
    Zeh., Jul 29, 2010 IP
  3. gary4gar

    gary4gar Peon

    Messages:
    502
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks a ton!
    that worked
    gary4gar, Jul 30, 2010 IP