Last active
December 15, 2015 12:19
-
-
Save bleeDev/5259062 to your computer and use it in GitHub Desktop.
Example of filtering an array with a closure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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