array_filter with a callback function
$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 c开发者_C百科reate a callback function and pass extra parameter ?
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'));
精彩评论