7

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?

1
  • 5
    What version of PHP are you using? Commented Jul 7, 2010 at 18:49

2 Answers 2

7

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.\'"\';')); 
Sign up to request clarification or add additional context in comments.

Comments

6

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

1 Comment

Oh, I had no idea this was a version issue. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.