array_map
accepts string as its first argument. Is there a way, to use arrays instead of strings, like:
.... array_map( array('trim','urlencode'), $my_array);
so I could attach multiple functions.
print_r( array_map ( "fun2" , $arr1 , $arr2 )); ?> Creating an array of arrays using array_map(): We can also use the array_map() function in PHP to create array of arrays. To do this we have to pass null as parameter in place of functionName parameter and the list of arrays to create an array of arrays.
The returned array will preserve the keys of the array argument if and only if exactly one array is passed. If more than one array is passed, the returned array will have sequential integer keys.
The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function. Tip: You can assign one array to the function, or as many as you like.
A Map is a sequential collection of key-value pairs, almost identical to an array used in a similar context. Keys can be any type, but must be unique. Values are replaced if added to the map using the same key.
You can define a function to combine these trim
and urlencode
functions. Then use the new function name or the new function as the first parameter of the array_map()
function.
array_map(function($v){
$v = trim($v);
$v = urlencode($v);
return $v
}, $array);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With