PDA

View Full Version : [PHP] Cleaning $_POST array using foreach()



redrum87
August 9th, 2006, 02:25 AM
How could I express this...

$_POST['first_name'] = addslashes($first_name);
$_POST['last_name'] = addslashes($last_name);
$_POST['email_address'] = addslashes($email_address);
$_POST['username'] = addslashes($username);
$_POST['info'] = addslashes($info);
...like this...


foreach($_POST as $input) {
$_POST['array_key'] = addslashes($input);
}
...where 'array_key' is an element of the $_POST array, without actually having to list out the $_POST array elements one by one. If this could be done, it could be put into a simple function that doesn't require any arguments.

redrum87
August 9th, 2006, 02:33 AM
Nevermind. PHP has the best function set ever...


array_walk($_POST, 'addslashes');

skOOb
August 9th, 2006, 08:06 AM
nice, Ive never seen that function. back to the php manual for me.

bwh2
August 9th, 2006, 09:34 AM
that's pretty cool. didn't know that existed either.

ZephyrWest
August 9th, 2006, 12:21 PM
Nice find! I'll have to remember that for future projects.

Jeff Wheeler
August 9th, 2006, 12:23 PM
I dislike how PHP doesn't have a real callback type… they make you quote the function… :(

redrum87
August 9th, 2006, 03:12 PM
Glad you all enjoyed it. I had never seen it either.

necrotic
August 10th, 2006, 10:18 AM
You gunna cry about it? It's not that bad. At least you can do it :P

Also, there are a lot of handy array functions: http://www.php.net/array