dk4210 Posted March 31, 2011 Share Posted March 31, 2011 Hello Guys, I have a question. I have the following code to grab all of my $_POST vars and run through my filter function foreach($_POST as $key => $value) { $mydata[$key] = filter($value); } Before using the foreach loop I was just doing the basic $var1 = $_POST['var1']; $var2 = $_POST['var2']; I'm going to have about 40 - 50 $_POST vars so my question is how do i return it as just a $var name instead of having to do this echo "<br><br><br>example1" . $mydata['var1']; echo "<br><br><br>example2" . $mydata['var2']; I guess its hard for me to explain.. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/232278-_post-and-foreach-loop-question/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 31, 2011 Share Posted March 31, 2011 You can use array_map() instead of your foreach() loop (which will leave the filtered results in the $_POST array). It will be 2-3 times faster and won't double the about of memory needed to store the data in memory. Why do you want to make individual variables? The $_POST variables are perfectly fine variables. If you still want to use individual variables, you should only convert expected variables (so that hackers posting data to your code cannot inject their variables into your code, possibly bypassing security on your site and taking it over) or you should insure that the variables you create have their own unique name-space so that they cannot overwrite any of your existing program variables. You can use extract() to do this on the original $_POST array or your intermediate $mydata array, but please use the EXTR_PREFIX_ALL parameter so that the created variables cannot overwrite any of your existing program variables. Quote Link to comment https://forums.phpfreaks.com/topic/232278-_post-and-foreach-loop-question/#findComment-1194892 Share on other sites More sharing options...
dk4210 Posted March 31, 2011 Author Share Posted March 31, 2011 Thanks for the quick reply. The reason I want to have individual vars is because when I found out about the foreach loop already had many vars that my function depends on. For example I have the following function //Add a flood control function to check to see if the title,body & member id match Flood_Control($ad_title,$member_id,$ad_body); Notice that I have the vars in there.. I hate to recode all that to this Flood_Control( $mydata['ad_title'], $mydata['memberid'], $mydata['ad_body']); Seems to defeat the purpose.. If I had a way that I could reference to a $var as opposed to $mydata['$var'] would be great.. Can you please give me an example of the array_map();? If you still want to use individual variables, you should only convert expected variables Please explain more on that. I am going to research this ---> EXTR_PREFIX_ALL Quote Link to comment https://forums.phpfreaks.com/topic/232278-_post-and-foreach-loop-question/#findComment-1194895 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.