...

View Full Version : Getting data out of $_POST using foreach()?



Pro3110
09-05-2005, 01:08 PM
:confused:

I am beyond confused.

<?php

foreach($_POST as $key =>$value){
foreach($value as $k =>$val){
$$k=$k;
$$val = $val;
}
}
echo $$val."Likes" $$val;
?>

My little test form has fields 'name' and 'stuff' I've typed in Page in the 'name' field and Cookies in the 'stuff' field.

I'm expectiong to see
Page Likes Cookies.

Instead I get an error "Invalid arrguement supplied in foreach() line 4. This error is displayed 3 times followed by "Likes"


So this is what I thought I knew...

POST is a multidimentional array, that contains the field names of the form as one index, and the value of those fields as a element inthe array

That is

$_POST[SUBMIT][name]=Page

Therefore

foreach ($_POST as $key =>$value)

should get me

$key = submit
$value[name] =Page

Obviously I am wrong....somewhere. PHP doesn't think $value is another array.

Help!!!

Page

NancyJ
09-05-2005, 01:42 PM
value isnt an array its the value of $key



<?php

foreach($_POST as $key =>$value){
$$key=$value
}
echo $name."Likes" $stuff;
?>

or


extract($_POST);
echo $name."Likes" $stuff;



echo $$val."Likes" $$val;
just doesnt make any sense at all if you were expecting "name likes stuff", $$val cant simultaneously = name and stuff

Fou-Lu
09-05-2005, 03:31 PM
Yep yep, other than using an extract function the best bet would be to create a recursive function for this. I say this as $_POST can have as many grandchildren as you want it to have, and in order to correctly extract the values you would need to recursively call them.

However, (theres always a however ;)), this may not be practical for you either. I myself wouldn't ever use a recursive function on an array such as this, simply because I would later evaluate what could possibly be an array.

The biggest part is your call to a variable variable, you might have a misconseption of how that one works. Though $$k can equal $k, this is doubtful what you are looking for.


$varname = 'Hello';
$$varname = 'World';

echo $Hello;

// Ouput: 'World';

Thats how the variable variables work, it assigns the value of the original variable as the name of the new variable.
Variable variables (http://ca3.php.net/manual/en/language.variables.variable.php), are an extremely handy thing in php.

marek_mar
09-05-2005, 05:15 PM
I'd watch out with those recursive functions. In PHP you can have recursive arrays.
I'd also watch out for variable variables as these produce hard to find errors.

Fou-Lu
09-05-2005, 05:39 PM
I'd watch out with those recursive functions. In PHP you can have recursive arrays.
I'd also watch out for variable variables as these produce hard to find errors.
Good point about the errors, you gotta stay on top of whats happening in order to avoid them.
As with the recursive array, can you explain? Offhand I can't quite put together 'recursive' and 'array' to provide myself with any mental feedback, lol. Chances are I've used whatever it is, but I just can't quite picture it. Can you give me a refreshing course?

marek_mar
09-05-2005, 06:17 PM
$GLOBALS is a recursive array. It's an array that has a reference to itself...


<?php
$a = array(1);
$a[] = &$a;
print_r($a);
?>

Pro3110
09-05-2005, 10:34 PM
value isnt an array its the value of $key



<?php

foreach($_POST as $key =>$value){
$$key=$value
}
echo $name."Likes" $stuff;
?>

:thumbsup: Okay, this is what I needed to see. Not sure where I got the bright idea that $value was another array. Too many late nights with the PC :D ?


echo $$val."Likes" $$val;
just doesnt make any sense at all if you were expecting "name likes stuff", $$val cant simultaneously = name and stuff

Well it made perfect sense at 3a.m. $$val should have a different value each time it was called cause I was iterating through an array! DOH! LOL. Anyway, I've got it now.

I'm sure I'll have more Green Horn Qs that are only slightly less silly than this one you've answered!

Thanks
Page

Pro3110
09-05-2005, 10:39 PM
I'd watch out with those recursive functions. In PHP you can have recursive arrays.
I'd also watch out for variable variables as these produce hard to find errors.

Can you elaborate or give an example?

Thanks
Page

marek_mar
09-05-2005, 11:06 PM
I've given two examples of recursive arrays in my previous post. The second point should be the result of the experiance you have gained by asking this question.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum