简体   繁体   English

检查所有数组值是否在范围内的最佳方法

[英]Best way to check if all array values are in a range

How is best way to check if all array values are in range? 如何检查所有数组值是否在范围内的最佳方法?

For example: 例如:

$range = range(10, 40);

$array1 = array(10, 20, 40); // OK 
$array2 = array(11, 22, 42, 30); // FALSE 
$array3 = array(50); // OK 
$array4 = array(10, 20, 30, 'a'); // FALSE 
$array5 = array("%", 20); // FALSE 
$array6 = array(10, 20, 40, 39, 40); // OK 

So how can I check if array values are numbers (integer) and are in range? 那么如何检查数组值是否为数字(整数)并且在范围内?

I tried: 我试过了:

$tmp = 0;
foreach ($array1 as $a1) {
   if (!in_array($a1, $range) && is_int($a1)) {
      $tmp++;
   }
}

if ($tmp > 0) {
   echo "false";
} else {
echo "ok";
}

Maybe is better way? 也许是更好的方法? Maybe with array_map() ? 也许与array_map()

    <?php
    $range      = range(10, 40);
    $array1     = array(10, 20, 40);
    $isInRange  = (min($array1)>=min($range) and max($array1)<=max($range)) ? "Yes" : "No";
    echo $isInRange;
    //prints 'Yes' when compared with array(10, 20, 40);
    //prints 'No' when compared with array(11, 22, 42, 30); as you have out range value 42 here
    ?>

A simplest way to check this is using max and min here, because you avoid validating for integers here. 最简单的检查方法是在此处使用maxmin ,因为在这里避免验证整数。 max selects the maximum value within the array, and min the least one. max选择数组中的最大值, min选择min

Would something like this do the job ? 这样的事情会做吗?

It compare every value in the array with the range ($min and $max), and return "Not in range" if one of the value in the array is not between the specified range. 它将数组中的每个值与范围($ min和$ max)进行比较,如果数组中的值之一不在指定范围内,则返回“不在范围内”。

$min = 10;
$max = 40;


$array1 = array(10, 20, 40); // OK 


foreach ($array1 as $tmpArr){
    if (!($min<=$tmpArr && $tmpArr<=$max)){
        echo "Not in range";
        break;
    }
}
function array_values_all_numbers($a, $min = PHP_INT_MIN, $max = PHP_INT_MAX){
  foreach($a as $v){
    if(is_int($v) && $v >= $min && $v <= $max){
      return false;
    }
  }

  return true;
}

var_dump(array_values_all_numbers(range(0, 100, 3))); // true
var_dump(array_values_all_numbers([1, 2, 3, 4, 'a'])); // false
var_dump(array_values_all_numbers([1, 2, 3, 4], 3, 4)); // false
var_dump(array_values_all_numbers([1, 2, 3, 4], 2, 4)); // true

Or perhaps an alternative: 或替代方法:

$myarray = array_filter(array_values($input), 'is_int');
foreach($myarray as $value){
  // loop over all array values that are numbers and skip the rest.
}

On a side-note; 在旁注; In terms of programming a range can mean a couple of things: 就编程而言, 范围可能意味着两件事:

  • Checking to see if a number is between two numbers. 检查数字是否在两个数字之间。
  • Range, looping start to end with an optional step . 范围,循环以可选步骤 开始结束
  • PHP function range() , returning an array from start to end with an optional step PHP功能range()开始返回一个数组结束与可选的步骤

It makes for a very confusing question. 这提出了一个非常令人困惑的问题。

Here's a fun way: 这是一种有趣的方式:

var_dump(
    array_values($array1) == array_filter(array_values(array_intersect($range, $array1)), 'is_int')
);
  • Compute the intersection between the range and array 计算范围和数组之间的交集
  • Remove non-integers 删除非整数
  • Compare with the array 与数组比较
  • Result is true or false 结果是true还是false

just run it and learn fro here. 只需运行它并在这里学习即可。

<?php
    $array1 = array(11, 20, 30); 
    $array2 = array(11, 22, 22, 30); 
    $array3 = array(20); 
    $array4 = array("a", 20, 30, 13); 
    $array5 = array(46, 20); 
    $array6 = array(52, 20, 31, 39, 30);

    $arr = array($array1, $array2, $array3, $array4, $array5, $array6);

    array_walk($arr, function($val){
        if(   min($val) > 10 && max($val) < 40){
            echo "<br/>matched for<br/>";
            print_r($val);
        } else {
            echo "<br/>not matched for<br />";
            print_r($val);
        }
    });
?>

There are a lot better way, you can try. 有很多更好的方法,您可以尝试。

Output 输出量

matched for
Array ( [0] => 11 [1] => 20 [2] => 30 ) 
matched for
Array ( [0] => 11 [1] => 22 [2] => 22 [3] => 30 ) 
matched for
Array ( [0] => 20 ) 
not matched for
Array ( [0] => a [1] => 20 [2] => 30 [3] => 13 ) 
not matched for
Array ( [0] => 46 [1] => 20 ) 
not matched for
Array ( [0] => 52 [1] => 20 [2] => 31 [3] => 39 [4] => 30 )

I assumed you have just a normal array not an associative array. 我假设您只有普通数组而不是关联数组。

<?php

$a = array(1,2,3);
$b = array(2,3);
$c = array(2,3,4);

function all_in_range(array $needle, array $haystack) {
    return array_reduce(
        $needle,
        function($carry, $item) use ($haystack){
            return $carry?in_array($item,$haystack):false;
        },
        true
    );
}

var_dump(all_in_range($b, $a)); // check $b inside $a - true
var_dump(all_in_range($c, $a)); // check $c inside $a - false

results: 结果:

bool(true)
bool(false)

=== or something like this === ===或类似===

function all_in_range(array $needle, $min, $max) {
    return ( is_int($min) && is_int($max) ) ?
        array_reduce(
            $needle,
            function($carry, $item) use ($min, $max){
                // limits values are included
                return $carry?is_int($item)?($item>=$min && $item<=$max):false:false;
            },
            true
        ) : 'wrong limits';
}

var_dump(all_in_range($b, 1, 3)); // check $b inside range 1..3
var_dump(all_in_range($c, 1, 3)); // check $c inside range 1..3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM