Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple functions using array_map [duplicate]

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.

like image 877
T.Todua Avatar asked May 14 '17 10:05

T.Todua


People also ask

How to use array_ map function in PHP?

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.

Does array map preserve keys?

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.

How does array_ map work?

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.

What is mapping in PHP?

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.


1 Answers

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);
like image 69
LF00 Avatar answered Sep 27 '22 19:09

LF00