operators as callbacks?
| From: | Joe | Date: | Sat, 29 Nov 2008 21:42:44 +0000 |
| Subject: | operators as callbacks? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-283917@lists.php.net to get a copy of this message | ||
Is it possible to use a PHP operator as a callback? Suppose I want to add two
arrays elementwise, I want to be able to do something like this:
array_map('+', $array1, $array2)
but this doesn't work as "+" is an operator and not a function.
I can use the BC library's math functions instead:
array_map('bcadd', $array1, $array2)
but that is an ugly hack that might break if the library is not available.
I can create an anonymous function:
array_map(create_function('$a, $b', 'return $a + $b;'), $array1, $array2)
but that seems really unnecessarily verbose.
Is there any simple clean way to do it? Thanks,