7

A quick question.

Is it possible to declare the callback function inline, in php? For example,

array_filter($input_array, "function($item) { $item['state'] != 0 }")
jose
  • 2,685
  • 4
  • 36
  • 48

4 Answers4

12

Yes, after php 5.3, you could use anonymous function.

array_filter($input_array, function($item) { return $item['state'] != 0; });
Tim Biegeleisen
  • 451,927
  • 24
  • 239
  • 318
xdazz
  • 154,648
  • 35
  • 237
  • 264
2

Sure it calls anonymous functions:

array_filter($input_array, function($item) { 
    return $item['state'] != 0;
});
Denis Ermolin
  • 5,410
  • 6
  • 26
  • 43
0
array_filter($input_array, function($item) { 
    return $item['state'] != 0;
});

This functionality is available from 5.3 or > version of php. In 5.4> version will support $this in inline Anonymous Functions

link for php callback > How do I implement a callback in PHP?

Community
  • 1
  • 1
PankajR
  • 332
  • 3
  • 12
0

with create_function? ex:

 $result = array_filter($array, create_function('$a','return preg_match("#\S#", $a);'));     
abrfra
  • 618
  • 2
  • 6
  • 24