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:
    <form method="post"  action="save.php"> 
    
    <table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
      <tr>
        <td colspan="3">&nbsp;</td>
      </tr>
      <tr>
        <td width="160" valign="top">
    
    <p>
    <strong>Select the most appropriate option</strong>
    
    <table border="0">
    <tr><td rowspan="4">
    
    <table border="0">
    <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>
    <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>
    <tr><td><input type="radio" name="question-15-answer" value="3" /></td><td>I'm really in between.</td></tr>
    </table>
    
    <br />
    
    <table border="0">
    <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>
    <tr><td><input type="radio" name="question-11-answer" value="2" /></td><td>I prefer one-on-one conversations.</td></tr>
    <tr><td><input type="radio" name="question-11-answer" value="3" /></td><td>I'm really in between.</td></tr>
    </table>
    <br />
    
    <snip>
    ......and so on 
    
    PHP:
    Now,to keep track of the actual answers I created an array which hold all the questions and their 3 respective options:

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

    Array
    (
        [question-15-order] => 0
        [question-15-answer] => 3
        [question-11-order] => 0
        [question-11-answer] => 3
    <snip>
    ......and so on 
    
    PHP:
    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:
    $selected = $_POST[$value];
    $choice = $questions[$key][$selected];
    echo $choice;
    PHP:
    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
    $questionsCount = count($questions);
    for ($i = 0; $i < $questionsCount; $i++) {
        $selected = $_POST['question-'.$i.'-answer'];
        $choice = $questions[$i][$selected];
        echo 'Answer '.$i.': '.$choice . '<br />';
    }
    ?>
    
    PHP:
    ?

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

    gary4gar Peon

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