0

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.

1

2 Answers 2

1

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 ;
}); 
Sign up to request clarification or add additional context in comments.

1 Comment

Great. Thanks! Works!
0

$lteam_id is not accessible in closure inside array_filter(). either pass the variable as argument of the closure or use using

$flt_lteam = array_filter($events, function($obj) using($lteam_id)

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

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.