1

I currently construct a multi-dim array from my input. like so: (example)

<form method=post action="testing.php">
<input name="response[0]['id']" type="hidden" value="<? echo $q1; ?>">
<input name="response[0]['answer']" type=text value=''>
<input name="response[1]['id']" type="hidden" value="<? echo $q2; ?>">
<input name="response[1]['answer']" type=text value=''>
<input name="response[2]['id']" type="hidden" value="<? echo $q3; ?>">
<input name="response[2]['answer']" type=text value=''>
<input name="response[3]['id']" type="hidden" value="<? echo $q4; ?>">
<input name="response[3]['answer']" type=text value=''>
<input type="submit" value="submit">
</form>

so that is successfully be POSTED. However I am trying to use a foreach to print out the values and I am getting it wrong.

EDIT my output array:

Array ( 
[0] => Array 
( 
['id'] => q1 
['answer'] => 1 
) 
[1] => Array 
( 
['id'] => q2 
['answer'] => 2 
) 
[2] => Array 
( 
['id'] => q3 
['answer'] => 3 
) 
[3] => Array 
( 
['id'] => q4 
['answer'] => 4 
)
) 

can somebody explain how i would extract the values with a foreach or even a better way?

many thanks

3 Answers 3

3
foreach ($_POST['response'] as $response) {
  echo $response['id'];
  echo $response['answer'];
}

This should do it.

Sign up to request clarification or add additional context in comments.

4 Comments

ive already set $response as $_POST['response'] before the foreach.
No problem, its up to you, how you name your variables :)
Dont know, what you have tried exactly, but if you name your array $response you must use another name for the iterator variable, for examle $entry: foreach($response as $entry) { echo $entry['id']; echo $response['answer']; }
i simply implemented your example (so directly using the $_POST array) no luck.
0

EDIT
Note the apos (') are part of the name! Either change the HTML (response[0][id]) or do the following.

The incoming array should look like:

$response = array(
    0 => array("'id'" => ..., "'answer'" => ...),
    1 => array("'id'" => ..., "'answer'" => ...),
    2 => array("'id'" => ..., "'answer'" => ...),
    3 => array("'id'" => ..., "'answer'" => ...),
);

Thus,

foreach ($response as $resp) {
    print 'ID=' . $resp["'id'"] . ', answer=' . $resp["'answer'"];
}

The problem: <input name="response[0]['answer']" /> will yield an array with 'answer' as key. I.e., the literal string with apos, not just answer. You should probably change the HTML to <input name="response[0][answer]" /> to avoid confusion. I will try and see if this is documented behavior. This behavior is indicated in the docs.

3 Comments

this is essentially what I tried and am getting no values out just the text.
@buymypies What does a var_dump() on $_POST (or $_POST['response']) give?
@buymypies You're very welcome. This was actually not just a look-in-the-manual question (which I first expected) :) Happy hacking.
0

is this what you're trying to do?

<?php foreach($response as $entry): ?>
    <input name="<?php echo $entry['id']; ?>" />
    <input name="<?php echo $entry['answer']; ?>" />
<?php endforeach; ?>

and ofcourse structure the inputs the way you would as above

5 Comments

each row has an id and an answer, So i want to get both for every row.
I spotted three mistakes in this example: you're not getting the value from $_POST, you foreach as $entry but use $response inside the loop and you spelled $response wrong.. ;)
im just going off the variables he's using.. who knows what framework he's using.. i dont use $_POST..
also his question is about extracting values of a 2 dimensional array which i am properly addressing. there's no errors.
if you look at my edit you can see the print_r output of my array after posting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.