This is a discussion on applying one function to all array elements within the PHP General forums, part of the PHP Programming Forums category; Hi, Can someone show me a simple, clean way to apply one function (eg stripslashes()) to every element in an ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
Can someone show me a simple, clean way to apply one function (eg stripslashes()) to every element in an array (eg $_POST)? I've been doing it with a foreach() loop, but there has to be a better way. I've also seen http://www.php.net/array_walk, but couldn't determine how/if it suited my needs. TIA Justin |
|
|||
|
>Can someone show me a simple, clean way to apply one function (eg
>stripslashes()) to every element in an array (eg $_POST)? > >I've been doing it with a foreach() loop, but there has to be a better >way. I've also seen http://www.php.net/array_walk, but couldn't >determine how/if it suited my needs. What part of "Apply a user function to every member of an array" makes it difficult to assess array_walk's usefulness? If it helps, array_map ("Applies the callback to the elements of the given arrays") does something similar. --------------------------------------------------------------------- michal migurski- contact info and pgp key: sf/ca http://mike.teczno.com/contact.html |
|
|||
|
--- Justin French <justin@indent.com.au> wrote:
> Can someone show me a simple, clean way to apply one function (eg > stripslashes()) to every element in an array (eg $_POST)? [snip] > I've also seen http://www.php.net/array_walk, but couldn't > determine how/if it suited my needs. Maybe you can explain how your need is not met with array_walk()? That would be my suggestion, since it seems to do exactly what you want, but then you mention that you have already looked at it. It does require that the function take two parameters, which isn't the case with stripslashes(). So, you can make a very tiny wrapper function that discards the second parameter (since you don't need it): <? function array_stripslashes(&$var, $key) { $var = stripslashes($var); } array_walk($_POST, 'array_stripslashes'); ?> Hope that helps. Chris ===== My Blog http://shiflett.org/ HTTP Developer's Handbook http://httphandbook.org/ RAMP Training Courses http://www.nyphp.org/ramp |