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,733
  • 4
  • 37
  • 51

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
  • 502,043
  • 27
  • 286
  • 360
xdazz
  • 158,678
  • 38
  • 247
  • 274
2

Sure it calls anonymous functions:

array_filter($input_array, function($item) { 
    return $item['state'] != 0;
});
Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
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
  • 340
  • 3
  • 12
0

with create_function? ex:

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