1

I have the following post string:

Name=John&Age=33&Question1=What's+your+gender%3F&Answer1=Male&Question2=What's
+your+education%3F&Answer2=Graduate

I'm going to have randomly generated amount of questions each time so I won't be able to know number of passed variables. Thus, I am searching for a solution something like:

foreach($postItem as $varr) {
    echo $varr["name"].": ".$varr["value"]."<br />";
}
Litestone
  • 529
  • 7
  • 22

2 Answers2

0

Post them as answer[1], answer[2] etc instead of answer1, answer2, and PHP will turn them into an array for you.

Andy
  • 49,085
  • 60
  • 166
  • 233
Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
0
//parse query string to an array ($output)
parse_str($postString, $output);

echo $output['Name'] . ': ' . $output['Age'] . '<br />';

//process questions
$i = 1;
while (array_key_exists('Question' . $i, $output)) {
    echo $output['Question' . $i] . ': ' . $output['Answer' . $i] . '<br />';
    $i++;
}

function used http://php.net/parse_str

Weltschmerz
  • 2,166
  • 1
  • 15
  • 18