Array_Map using multiple native callbacks?

I want to run 3 own functions in one array: trim, strtoupper and mysql_real_escape_string. It can be done?

Trying to pass an array as a callback like this does not work:

$exclude = array_map(array('trim','strtoupper','mysql_real_escape_string'), explode("\n", variable_get('gs_stats_filter', 'googlebot'))); 

Although this works just fine, because it uses only one built-in function as a callback:

 $exclude = array_map('trim', explode("\n", variable_get('gs_stats_filter', 'googlebot'))); 
+7
source share
3 answers

You will need to do this as follows:

 $exclude = array_map(function($item) { return mysql_real_escape_string(strtoupper(trim($item))); }, explode("\n", variable_get('gs_stats_filter', 'googlebot'))); 

(This example requires PHP 5.3+ as it uses anonymous functions)

+10
source

Yes, just pass the result of one match to another:

 $result = array_map( 'mysql_real_escape_string', array_map( 'trim', array_map( 'strtoupper', $your_array ) ) ); 

You can also use the callback in PHP 5.3+:

 $result = array_map(function($x){ return mysql_real_escape_string(trim(strtoupper($x))); }, $your_array); 

or earlier (in PHP versions below 5.3):

 $result = array_map( create_function('$x','return mysql_real_escape_string(trim(strtoupper($x)));'), $your_array ); 
+5
source

You can also do something like:

  $exclude = array_map(function($item) { return trim(strtoupper(mysql_real_escape_string($item))); }, explode(...)); 

or something like that. Pass an anonymous function that does all this.

Hope this helps.

Good luck :)

+4
source

All Articles