Consider the following:

function bracketStrip($arrItem){
	return trim($arrItem, '()');
}

$arr = array('(20)','(15)','(6)','(1)');
$arr = array_map("bracketStrip", $arr);
echo '<pre>'.print_r($arr, true);

Which outputs the values with bracket removed. Fair enough. We can clearly see what's going on.

Now, I have often pondered.. could I not simply integrate that function into the array_map itself. So my first thought is, perhaps using the lambda style create_function().. but then I automatically ask, could I use create_function as a callback? According to this page in the php manual, apparently, yes.

"Apart from common user-defined function, create_function() can also be used to create an anonymous callback function."

Yet I look at the examples shown, and I cannot find the format I have in mind, which is something like this:

array_map("create_function(return trim($arr, '()')", $arr);

With the above line, I get the following warning message:
"Warning: array_map() [function.array-map]: The first argument, 'create_function(return trim(Array, '()')', should be either NULL or a valid callback"

So I am asusming one of two things here.. either
a) It is syntactically incorrect (as in, it is an invalid callback) - or -
b) simply not possible

Any thoughts or suggestions? The whole idea here is since I only want to trim something, I'm trying to integrate it into a function that could be streamlined within the array_map function.

    It would be something like:

    $arr = array_map(create_function('$val', 'return(trim($val, "()"));'), $arr);
    

    Hopefully I got all the parentheses in the right place.

      NogDog;10899314 wrote:

      It would be something like:

      $arr = array_map(create_function('$val', 'return(trim($val, "()"));'), $arr);
      

      Hopefully I got all the parentheses in the right place.

      Perfecto-mondo! You got it just right.
      Much appreaciated!

      So indeed it was bad formatting all along. I'll definitely have to make note of this for next time.

      Thanks again.

      Cheers

        Meanwhile, in PHP 5.3 it's even simpler....

        $arr= array_map(function($val) {
            return trim($val, '()');
        }, $arr);

        Bonuses are that the callback can be syntax-checked at load time, bytecode compilers can optimise it, and in-scope variables can be closed over.

        Mmmm.....

          Weedpacket;10899329 wrote:

          Meanwhile, in PHP 5.3 it's even simpler....

          $arr= array_map(function($val) {
              return trim($val, '()');
          }, $arr);

          Bonuses are that the callback can be syntax-checked at load time, bytecode compilers can optimise it, and in-scope variables can be closed over.

          Mmmm.....

          That's good to know (when 5.3 becomes availble as a stable release [i.e, exits RC] and hosting providers update their version to reflect it)!

          So does the function() in this case replace create_function as of version 5.3? Or asked another way, if so, does this mean as of 5.3, create_function will be depreciated in favor of function?

            I don't know what the official position on create_function would be, but I can't think of any use for it that proper lambda functions like 5.3 introduces can't handle more elegantly - and plenty of uses for the latter where the former is a royal pain.

            I've been looking through some stuff and about the only place where it might matter is where the source of the created function is built up piece by piece before being passed to create_function - and even that is almost certainly due to the constraints of needing to create it as a string of source code.

              Ok. Thanks again for the heads up, WP. Nice to know those things. I'll definitely have to look into function once 5.3 arrives. Certainly looks easier to work with than create_function.

                Write a Reply...