I'm using array_map to trim all my array values but I need to pass a third parameter because I need to more than just trim whitespaces so I'm passing in a third parameter. Basically I want to trim all array values of whitespaces, single quotes, and double quotes.
I have a utility class where I created the function and it looks like this:
public function convertToArray($string, $trim = false) {
$split = explode(",", $string);
if($trim) {
$split = array_map("trim", $split, array(" '\""));
}
return $split;
}
Somehow I can't make this work though. I can still see double quotes in the result even though I followed the answer here.
I even tried
if($trim) {
$split = array_map("trim", $split);
$split = array_map("trim", $split, array("'"));
$split = array_map("trim", $split, array('"'));
}
but I still get the same result.
array_maptakes a function that takes only one parameter. If you want to map your array withtrim()with subsequent parameters different from the default ones, you have to wrap it with an anonymous function: