开发者

PHP Anonymous function with array_walk

开发者 https://www.devze.com 2023-01-05 21:18 出处:网络
I\'m trying to use array_walk with an anonymous function, but I always get the error // Parse error: syntax error, unexpected T_FUNCTION in ... on line X

I'm trying to use array_walk with an anonymous function, but I always get the error

 // Parse error: syntax error, unexpected T_FUNCTION in ... on line X
 if(!empty($myArray)) {
   array_walk($myArray, function(&$value, $key){ // Line X
     $value = '"'.$value.'"'; // Add quotes
   });
 }

The surrounding file synt开发者_C百科ax is correct. Any thoughts?


Yes, true anonymous functions (closures) are only available from PHP 5.3, however you can still create an anonymous function in earlier versions of PHP using the create_function() call, which can be used with array_walk(). Something like:

array_walk($myArray, create_function('&$value,$key', '$value = \'"\'.$value.\'"\';'));


Check your PHP version... Anonymous functions are only available since 5.3...

0

精彩评论

暂无评论...
验证码 换一张
取 消