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 }")

4 Answers 4

15

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

array_filter($input_array, function($item) { return $item['state'] != 0; });
Sign up to request clarification or add additional context in comments.

3 Comments

Not my case unfortunately.. 5.1 here :(
@jose Then you could use create_function php.net/manual/en/function.create-function.php
Note that each line of code inside the anonymous function needs to end with a semicolon.
2

Sure it calls anonymous functions:

array_filter($input_array, function($item) { 
    return $item['state'] != 0;
});

Comments

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?

1 Comment

this is an identical answer to the already accepted answer. Please avoid doing that.
0

with create_function? ex:

 $result = array_filter($array, create_function('$a','return preg_match("#\S#", $a);'));     

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.