PHP is there a true() function?

Posted by Gremo on Stack Overflow See other posts from Stack Overflow or by Gremo
Published on 2011-11-29T17:44:04Z Indexed on 2011/11/29 17:50 UTC
Read the original article Hit count: 295

Filed under:
|
|

I'm writing a function to check all elements inside an array, returning a single boolean value. is there a true() function?

function all($f, array $arr)
{
    return empty($arr) ? false : array_reduce($arr, function($v1, $v2) use ($f) {
        return $f($v1) && $f($v2);
    }, true);
}

$test = array(1, 6, 2);
$gte0 = function($v) { return $v >= 0; }

var_dump(all($gte0, $test)); // True

$test = array(true, true, false);
$id   = function($v) { return $v; } // <-- this is what i would avoid
var_dump(all($id, $test)); // False

all(true, $test); // NOT WORKING because true is not a function

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays