0
$thisQuestion = array_filter($pollQuestions,function($q) use ($questDataArr){
                      return $questDataArr[0] == $q["id"];
                    });

As I am using 2 variables here I was using this inline function . How can i create a callback function and pass extra parameter ?

1
  • 1
    You pass extra parameters with use(), like you have done. It would help if you showed us what those data types looked like. Commented Sep 2, 2011 at 21:57

1 Answer 1

2

If I understand your question correctly:

  • you don't want to use an anonymous function
  • you need a function that keeps some state with it

The solution is to create a class:

class MyCallback {
    private $questDataArr;
    public function __construct($questDataArr) {
        $this->questDataArr = $questDataArr;
    }
    function callback($q) {
        return $this->questDataArr[0] == $q["id"];
    }
}

array_filter($pollQuestions, array(new MyCallback($questDataArr), 'callback'));
Sign up to request clarification or add additional context in comments.

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.