Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 syntax is correct. Any thoughts?

share|improve this question
5  
What version of PHP are you using? – Jon Cram Jul 7 '10 at 18:49

2 Answers

up vote 6 down vote accepted

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.\'"\';'));
share|improve this answer

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

share|improve this answer
Oh, I had no idea this was a version issue. Thanks! – ash Jul 7 '10 at 18:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.