PHP function to check array

Is there a function that could give an array that will return true if the provided function returned for all of them?

theFunction(array(1,2,3) , 'is_numeric') //true
theFunction(array(1,"b",3) , 'is_numeric') //false
+3
source share
5 answers

No, but you can use array_reduce:

array_reduce(array(1,2,3),
    function ($a, $v) { return $a && is_numeric($v); }, true);

You can, of course, create your own higher order function:

function for_all(array $arr, $func) {
    return array_reduce($arr,
        function ($a, $v) use ($func) {
            return $a && call_user_func($func, $v);
        }, true);
}

var_dump(
    for_all(array(1,2,3), 'is_numeric')
); //true
+5
source

array_filter() performs the task:

$data = array(1, 2, 3);

if ($data === array_filter($data, 'is_numeric'))
{
    // all values of $data are numeric
}
+4
source
 /**
  * all in collection?
  *
  * Passes each element of the collection to the given function. The method
  * returns true if the function never returns false or null.
  * 
  * If the function is not given, an implicit
  * function ($v) { return ($v !== null && $v !== false) is added
  * (that is all() will return true only if none of the collection members are false or null.)
  *
  * @param array $arr input array
  * @param function $lambda takes an element, returns a bool (optional)
  * @return boolean
  */
 function all($arr, $lambda=null) {
     // these differ from PHP "falsy" values
     if (!is_callable($lambda)) {
         foreach ($arr as $value)
             if ($value === false || $value === null)
                 return false;
     } else {
         foreach ($arr as $value)
             if (!call_user_func($lambda, $value))
                 return false;
     }
     return true;
 }

Ruby enum

:

var_dump(all($array, 'is_numeric'));
var_dump(all($array, 'is_string'));
var_dump(all($array, function($x) { return $x != 'fun';})); // PHP >= 5.3.0
+2

, min array_map .

(bool)min(array_map('is_numeric', array(1,2,3))); //true
(bool)min(array_map('is_numeric', array(1,"b",3))); //false

, , , , .

!array_filter('is_not_numeric', array(1,2,3)); //true
!array_filter('is_not_numeric', array(1,"b",3)); //true
+2

This is a function for checking values โ€‹โ€‹according to validation rules or just using callers: names of PHP functions or closures.

/**
* Returns true if $value matches $pattern
*
* @param $value
* @param string $pattern
*
* @return bool
*
* @see https://github.com/ptrofimov/matchmaker - ultra-fresh PHP matching functions
* @author Petr Trofimov <petrofimov@yandex.ru>
*/
function matcher($value, $pattern)
{
$args = [];
if (($p = ltrim($pattern, ':')) != $pattern) foreach (explode(' ', $p) as $name) {
    if (substr($name, -1) == ')') {
        list($name, $args) = explode('(', $name);
        $args = explode(',', rtrim($args, ')'));
    }
    if (is_callable(rules($name))) {
        if (!call_user_func_array(rules($name), array_merge([$value], $args))) {
            return false;
        }
    } elseif (rules($name) !== $value) {
        return false;
    }
} else {
    return $pattern === '' || $value === $pattern;
}

return true;
}

You can use it with a prepared set of validation rules implemented in the matchmaker project: https://github.com/ptrofimov/matchmaker

-1
source

Source: https://habr.com/ru/post/1758573/


All Articles