0

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.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Hanner
  • 371
  • 3
  • 9

1 Answers1

2

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.

Qirel
  • 25,449
  • 7
  • 45
  • 62
KiwiJuicer
  • 1,952
  • 14
  • 28