问题
I am trying to use array_filter with call back in php 5.2, but I get the following error:
Parse error: syntax error, unexpected T_FUNCTION
And I did search the solution using the error in Google search
and found that Php 5.2
does not support callback
. The code I am working on is:
$result = array_filter($lines, function($line) {
return stripos($line,"ID:")!==false;
});
How do I change it so that It can work in php 5.2
? Any help and workaround would be very much appreciated. Thanks.
回答1:
Anonymous functions was introduced in PHP 5.3, so if you are using PHP 5.2 or lower you need to define the function explicitly and pass the functions name as the second argument of array_filter()
, as shown below.
$result = array_filter($lines, 'filter');
function filter($line) {
return stripos($line,"ID:") !== false;
}
Consider upgrading to a newer version of PHP if you can.
- PHP.net on array_filter()
- PHP.net on anonymous functions
- PHP.net on the Closure class
来源:https://stackoverflow.com/questions/37985222/how-to-use-array-filter-with-callback-in-php-5-2