简体   繁体   English

如何确定整数是否在范围内并存储到两个数组中?

[英]How to determine if integers are within a range and store into two arrays?

I have an array called $grades which contains grades ranging from negative numbers to numbers over 100. 我有一个名为$grades的数组,其中包含从负数到超过100的数字范围的分数。

I need to put the negative grades and the grades over 100 into their own array and all the other numbers into their own array. 我需要将负分数和超过100的分数放入自己的数组中,并将所有其他数字放入他们自己的数组中。

for($i = 0; $i < count($grades); $i++) {
    if ($grades[$i] < 0 || $grades[$i] > 100) {
        $invalidGrades[] = $i;
    } else if ($grades[$i] >= 0 && $grades[$i] <= 100) {
        $validGrades[] = $i;
    }
}

This is the code I have so far. 这是我到目前为止的代码。 It appears to only be returning the numbers in the count instead of the values. 它似乎只返回计数中的数字而不是值。

could be you want assign the grades using same index you can try this way 可能是您想使用相同的索引分配成绩,您可以尝试这种方式

for($i = 0; $i < count($grades); $i++) {

      if ($grades[$i] < 0 || $grades[$i] > 100) {
          $invalidGrades[$i] =  $grades[$i];
      } else if ($grades[$i] >= 0 && $grades[$i] <= 100) {
          $validGrades[$i] = $grades[$i];
      }
}

or if you want push the value 或者如果您想推动价值

for($i = 0; $i < count($grades); $i++) {

      if ($grades[$i] < 0 || $grades[$i] > 100) {
          $invalidGrades[] =  $grades[$i];
      } else if ($grades[$i] >= 0 && $grades[$i] <= 100) {
          $validGrades[] = $grades[$i];
      }
}

Try This 尝试这个

$grades = array(88,86,90,78,95,101,78,105,79,110);
$invalidGrades = array();
$validGrades = array();

foreach($grades as $grade) {
    if ($grade > 100 || $grade < 0) {
        array_push($invalidGrades, $grade);
    } else {
        array_push($validGrades, $grade);
    }
}

print "Grades";
print_r($grades);
print "invalidGrades";
print_r($invalidGrades);
print "validGrades";
print_r($validGrades);

You should get the following. 您应该获得以下内容。

php54s test.php 
Content-type: text/html

GradesArray
(
    [0] => -1
    [1] => -4
    [2] => -10
    [3] => 88
    [4] => 86
    [5] => 90
    [6] => 78
    [7] => 95
    [8] => 101
    [9] => 78
    [10] => 105
    [11] => 79
    [12] => 110
)
invalidGradesArray
(
    [0] => -1
    [1] => -4
    [2] => -10
    [3] => 101
    [4] => 105
    [5] => 110
)
validGradesArray
(
    [0] => 88
    [1] => 86
    [2] => 90
    [3] => 78
    [4] => 95
    [5] => 78
    [6] => 79
)

foreach is your best option here, try this: foreach是您的最佳选择,请尝试以下操作:

//Some random grades
$grades = [1, -10, 90, 30, -3, 200, 300, 44];
$validGrades = array();
$inValidGrades = array();

foreach($grades as $grade){
  if($grade < 0 || $grade > 100){
    $inValidGrades[] = $grade;
  }else{
    $validGrades[] = $grade;
  }
}

//Test the output
echo "<pre>";
print_r($grades);
echo "<br>";
print_r($validGrades);
echo "<br>";
print_r($inValidGrades);

The output will be: 输出将是:

Array
(
    [0] => 1
    [1] => -10
    [2] => 90
    [3] => 30
    [4] => -3
    [5] => 200
    [6] => 300
    [7] => 44
)

Array
(
    [0] => 1
    [1] => 90
    [2] => 30
    [3] => 44
)

Array
(
    [0] => -10
    [1] => -3
    [2] => 200
    [3] => 300
)

This can be done with a simple if-else block. 这可以通过一个简单的if-else块来完成。 The only choice about how to construct it comes down to if you want to use one condition or two in the if expression. 有关如何构造它的唯一选择取决于您是否要在if表达式中使用一个或两个条件。

Method #1: ( Output Demo ) ( Calculation Demo ) "Single Condition" 方法1 :( 输出演示 )( 计算演示 )“单一条件”

$grades=array(-4,-1,0,1,6,10,70,99,100,101,105,110);
foreach($grades as $grade){
    if( ($grade+1)*(101-$grade) > 0 ){  // ($grade minus negative [lower limit])*([upper limit] plus $grad)
        $validGrades[] = $grade;
    }else{
        $invalidGrades[] = $grade;
    }
}
var_export($validGrades);
echo "\n---\n";
var_export($invalidGrades);

Method #2: ( Output Demo ) "Double Condition" 方法2 :( 输出演示 )“双重条件”

$grades=array(-4,-1,0,1,6,10,70,99,100,101,105,110);
foreach($grades as $grade){
    if( $grade<0 || $grade>100 ){
        $validGrades[] = $grade;
    }else{
        $invalidGrades[] = $grade;
    }
}
var_export($validGrades);
echo "\n---\n";
var_export($invalidGrades);

Output (from either method) 输出(从任何一种方法)

array (
  0 => -4,
  1 => -1,
  2 => 101,
  3 => 105,
  4 => 110,
)
---
array (
  0 => 0,
  1 => 1,
  2 => 6,
  3 => 10,
  4 => 70,
  5 => 99,
  6 => 100,
)

*Notes: *笔记:

  1. There will be an unnoticeable difference in efficiency between the two methods; 两种方法之间的效率会有明显的不同。 this is purely demonstrative. 这纯粹是示范性的。

  2. The "Double Condition" can also be written with the "And" logical operator ( && ) as if( $grade<=100 && $grade>=0 ){ with the same effect. 也可以使用“ And”逻辑运算符( && )来编写“ Double Condition”, if( $grade<=100 && $grade>=0 ){具有相同的效果。

  3. Because the pass / fail comparison is determined in the if expression, there is no need to use an elseif expression -- this would be redundant. 因为通过 / 失败比较是在if表达式中确定的,所以不需要使用elseif表达式-这将是多余的。

  4. foreach() is a more appropriate construct to loop with versus for because calling the count() function is avoided. 因为可以避免调用count()函数for所以foreach()是一种更适合与vs循环的结构。 This also prevents your coding error while trying to access the grades via their index. 这也可以防止在尝试通过成绩索引访问成绩时出现编码错误。

  5. Calling array_push() (as some other answers do) is unnecessary overhead -- not that it is actually going to impact your application. 调用array_push() (就像其他一些答案一样)是不必要的开销-并不是实际上会影响您的应用程序。 I like to avoid function calls when possible. 我喜欢在可能的情况下避免函数调用。 Using [] syntax is the function-free alternative to array_push() . 使用[]语法是array_push()的无功能替代方法。

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

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