Bluehost.com Web Hosting $6.95

applying one function to all array elements

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-03-2003
Justin French
 
Posts: n/a
Default applying one function to all array elements

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
Reply With Quote
  #2 (permalink)  
Old 11-03-2003
Mike Migurski
 
Posts: n/a
Default Re: [PHP] applying one function to all array elements

>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
Reply With Quote
  #3 (permalink)  
Old 11-03-2003
Chris Shiflett
 
Posts: n/a
Default Re: [PHP] applying one function to all array elements

--- 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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:33 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0