I have this little function to filter my array by keys:
private function filterMyArray( )
{
function check( $v )
{
return $v['type'] == 'video';
}
return array_filter( $array, 'check' );
}
This works great but since I have more keys to filter, I was thinking in a way to pass a variable from the main function: filterMyArray($key_to_serch)
without success, also I've tried a global variable, but seems not work.
Due some confusion in my question :), I need something like this:
private function filterMyArray( $key_to_serch )
{
function check( $v )
{
return $v['type'] == $key_to_serch;
}
return array_filter( $array, 'check' );
}
Any idea to pass that variable?
This is where anonymous functions in PHP 5.3 come in handy (note the use of
use
):Here's a PHP < 5.3 version using create_function.
You need to use the
use
keyword to get the variable in scope, c.f. this example in php's doc.should work because the inner function has access to the variables in the outer function