Bendude14 Posted September 12, 2008 Share Posted September 12, 2008 Could someone please tell me what would be the easiest way to retrieve everything from a post array. so instead of $var1 = $_POST['var1'] $var2 = $_POST['var2'] $var3 = $_POST['var3'] $var4 = $_POST['var4'] $var5 = $_POST['var5'] something like this? foreach($_POST as $var) { for($i=0; $var[$i]; $i++) { $var = $_POST[$var[$i]]; } } the information been posted is var1, var2, var3 etc Thanks Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted September 12, 2008 Share Posted September 12, 2008 something like this? foreach($_POST as $var) { for($i=0; $var[$i]; $i++) { $var = $_POST[$var[$i]]; } } why not try it? Quote Link to comment Share on other sites More sharing options...
Bendude14 Posted September 12, 2008 Author Share Posted September 12, 2008 i already did. suppose i should have mentioned that i just get a heap of undefined indexes. also my code would only assign the $_POST[$var[$i]] to $var when i need it to assign to var1, var2 etc hope this makes sense. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 12, 2008 Share Posted September 12, 2008 $_POST['var1'], $_POST['var2'], ... are already variables. Making a copy into $var1, $var2, ... is a waste of memory and processor cycles. Doing what you want would require using variable variables, which are three times slower than just accessing the array variable itself. Just use the foreach() loop to iterate over each element and do what ever you want with the key/value pairs inside the loop - foreach($_POST as $key => $value) { echo $key . $value; // process each key/value as needed } If the form fields var1, var2,... are actually a set of same type values, it is also much easier to make them an array with some base name and then you can iterate over that array using a foreach() loop as well. If that does not accomplish what you need in your code, explain exactly what you are doing or provide actual code and someone can provide direction on how to do it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.