Skip to content

Instantly share code, notes, and snippets.

@bleeDev
Last active December 15, 2015 12:19
Show Gist options
  • Save bleeDev/5259062 to your computer and use it in GitHub Desktop.
Save bleeDev/5259062 to your computer and use it in GitHub Desktop.
Example of filtering an array with a closure
<?php
//Since PHP 5.3: http://www.php.net/manual/en/functions.anonymous.php
$array_to_be_filtered = array(
1 => 'apple',
2 => 'orange',
3 => 'pinapple',
4 => 'plum',
5 => 'tangelo',
6 => 'plum'
);
$closure = filter_closure('plum');
$array_filtered = array_filter($array_to_be_filtered, $closure);
print_r($array_filtered);
function filter_closure($val){
return function($row) use($val) {
return $row == $val;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment