PHP is there a true() function?

I'm writing a function named all to check all elements inside an array $arr, returning a single boolean value (based of $f return value). This is working fine passing custom functions (see the code with $gte0 been passed to all).

However sometimes one want just check that an array contains all true values: all(true, $arr) will not work becase true is passed as boolean (and true is not a function name). Does PHP have a native true() like 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 passed as boolean
all('true', $test); // NOT WORKING because true is not a function

EDIT: another way could be checking $f in all function:

$f = is_bool($f) ? ($f ? function($v) { return $v; }
    : function($v) { return !$v; } ): $f;

After adding this, calling all with true is perfectly fine.

Answers


intval might do what you're looking for (especially as the array only contains integers in your example):

var_dump(all('intval', $test)); // False

However, many types to integer conversions are undefined in PHP and with float this will round towards zero, so this might not be what you want.

The more correct "function" would be the opposite of boolean true: empty, but it's not a function, so you can't use it (and invert the return value):

var_dump(!all('empty', $test)); // Does not work!

And there is no function called boolval or similar in PHP, so write it yourself if you need it ;)

Additionally your all function could be optimized. While iterating, if the current result is already FALSE, the end result will always be FALSE. And no need to call $f() n * 2 times anyway:

function all($f, array $arr)
{
    $result = (bool) $arr;
    foreach($arr as $v) if (!$f($v)) return FALSE;
    return $result;
}

Edit: knittl is right pointing to array_filter, it converts to boolean with no function given which seems cool as there is no "boolval" function:

function all($f, array $arr)
{   
    return ($c = count($arr))
        && ($f ? $arr = array_map($f, $arr) : 1)
        && $c === count(array_filter($arr));
}

var_dump(all(0, $test)); // False

Making the first function parameter optional will do you a proper bool cast on each array element thanks to array_filter.


Better to pass in a function to map the values to booleans that you can then reduce to a final value.

function all($map, $data) {
  if (empty($data)) { return false; }
  $reduce = function($f, $n) {
    return $f && $n;
  };
  return array_reduce(array_map($map, $data), $reduce, true);
}

$gte0 = function($v) { return $v >= 0; };
$gte2 = function($v) { return $v >= 2; };

$data = array(1, 2, 3, 4, 5);
var_dump(all($gte0, $data));
var_dump(all($gte2, $data));

Then the result of the function remains expectant but the test can be slotted in as needed. You can go a step further and allow both the map and reduce function to be passed in.

function mr($map, $reduce, $data) {
  return array_reduce(array_map($map, $data), $reduce, true);
}

Need Your Help

margin within a subset plot

r graphics plot margin

When we select the entire data range, we get a plot with a slight margin inside the plot region so that the plot doesn't touch the bounding box. However when we subset a plot, the margin is gone. Is

Axis2 with XMLBeans data binding - Problem with a java client generated from a XSD schema with anyType

java xsd axis2 xmlbeans

I have generated a client using Axis2 framework with XMLBeans as the data binding method.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.