public
Last active

  • Download Gist
array_all_and_array_any.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
/*
* Two annoying omissions from PHP are the lack of array_all() and array_any(), which check if
* a condition represented by a callback holds for all or any of the elements in an array.
*/
 
function array_all(array $arr, $cb) {
foreach ($arr as $e) {
if (!call_user_func($cb, $e)) {
return false;
}
return true;
}
}
 
function array_any(array $arr, $cb) {
foreach ($arr as $e) {
if (call_user_func($cb, $e)) {
return true;
}
return false;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.