kgaughan (owner)

Revisions

  • 6ee64b Wed Jan 07 09:49:03 -0800 2009
gist: 44350 Download_button fork
public
Public Clone URL: git://gist.github.com/44350.git
Embed All Files: show embed
array_all_and_array_any.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;
    }
}