this could be a very silly question but I just can't understand how PHP scope is working on this piece of code:
$leagueKey = 'NFL';
$response['response'] = array_filter($response['response'], function($tier){
return ($tier['LeagueKey'] === $leagueKey ? true : false);
});
When I run that, I get an "Undefined variable: leagueKey" exception. On the other hand, this works perfectly well:
$response['response'] = array_filter($response['response'], function($tier){
return ($tier['LeagueKey'] === 'NFL' ? true : false);
});
Why can't PHP see my $leagueKey variable inside the array_filter function?
Thanks!
$leagueKeyis defined outside of that function. In order to use it you could useglobal $leagueKeyinside your anonymous function but it's not the best of ways$leagueKeyis outside of the function, the string'NFL'is in, so it works.