Skip to content

Instantly share code, notes, and snippets.

@seejohnrun
Created March 13, 2013 15:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seejohnrun/5153491 to your computer and use it in GitHub Desktop.
Save seejohnrun/5153491 to your computer and use it in GitHub Desktop.
This should really just be part of PHP
<?php
// implementation
function array_partition($arr, $callable) {
$r = array(array(), array());
foreach ($arr as $e) {
$r[$callable($e) ? 0 : 1][] = $e;
}
return $r;
}
// usage
$arr = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
list($even, $odd) = array_partition($arr, function ($e) {
return $e % 2 == 0;
});
print_r($even); // 0, 2, 4, 6, 8
print_r($odd); // 1, 3, 5, 7, 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment