Page 1 of 1
Using functions on arrays
Posted: Sun Aug 03, 2003 5:42 pm
by werlop
Hi, I had a thought, and I am wondering if it is possible.
Can you do something like
Where $data is an array, or do you have to run the function on each element? e.g. strip all slashes from all elements in the array in one go.
Knowing me its probably complete rubbish, but there's no harm in asking!

Re: Using functions on arrays
Posted: Sun Aug 03, 2003 6:46 pm
by jmarcv
werlop wrote:... Where $data is an array, or do you have to run the function on each element? e.g. strip all slashes from all elements in the array in one go.
Not rubbish. It's called array_walk. Never used it myself, but you can read up on it here:
http://www.php.net/manual/en/function.array-walk.php
You'd be surprised what you can find on that site.
I finally broke down and went on a few years back, and couldn't believe all the new time savers they stuck in there that I was wasting time writing around.
Posted: Sun Aug 03, 2003 7:57 pm
by McGruff
Magic quotes settngs can do it all automatically, but it's probably better off if you have a choice.
Without that, yes you have to strip each element individually, one way or another, but a user-defined function can clean up the code a bit. Another idea:
foreach and pass $array by ref:
Code: Select all
<?php
function stripper(&array)
{
foreach ($array as $key=>$value)
{
$array[$key] = stripslashes($value);
}
}
// for example:
$result = mysql_fetch_array($query);
stripper($result);
?>
Posted: Mon Aug 04, 2003 6:08 pm
by werlop
Thanks to you both, it seems to me like I spend all my time cleaning up data from the database, this will help a lot!
