doudun1029 2017-06-04 17:25
浏览 28
已采纳

php数组过滤函数问题与变量

I have a problem using a variable inside this array filter:

$lteam_id = 25;
$flt_lteam = array_filter($events, function($obj)

{
return $obj['team_id'] == $lteam_id && $obj['type'] == 'kick' && $obj['minute'] <= 75 ;
}); 

As soon as I replace $lteam_idwith 25 it works and I get a result. Using the variable results in an array(0) { }... Hope you can help me here on using the variables correctly.

  • 写回答

2条回答 默认 最新

  • dshfjsh_5455 2017-06-04 17:36
    关注

    You use anonymous function, so you can use use statement to pass variable to it:

    $lteam_id = 25;
    $flt_lteam = array_filter($events, function($obj) use ($lteam_id)
    {
        return $obj['team_id'] == $lteam_id && $obj['type'] == 'kick' && $obj['minute'] <= 75 ;
    }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?