0

我试图弄清楚如何使用 for 循环来循环遍历数组并测试两个条件。我已经用 foreach 循环完成了它,但试图用 for 循环完成它。下面是我一直在研究的 if 块和 for 循环。

        if (empty($scores[0]) ||
        empty($scores[1]) ||
        empty($scores[2]) ||
        !is_numeric($scores[0]) ||
        !is_numeric($scores[1]) ||
        !is_numeric($scores[2])) {
            $scores_string = 'You must enter three valid numbers for scores.';
            break;
    }

这是HTML和PHP。

HTML:

<form action="." method="post">
    <input type="hidden" name="action" value="process_scores" />

    <label>Choose action:</label><br />
    <input class="radio" type="radio" name="calculate" value="average" checked="checked">Average<br />
    <input class="radio" type="radio" name="calculate" value="total">Total<br />
    <input class="radio" type="radio" name="calculate" value="both">Both<br />

    <label>Score 1:</label>
    <input type="text" name="scores[]"
           value="<?php echo $scores[0]; ?>"/><br />

    <label>Score 2:</label>
    <input type="text" name="scores[]"
           value="<?php echo $scores[1]; ?>"/><br />

    <label>Score 3:</label>
    <input type="text" name="scores[]"
           value="<?php echo $scores[2]; ?>"/><br />

    <label>&nbsp;</label>
    <input type="submit" value="Process Scores" /><br />

    <label>Scores:</label>
    <span><?php echo $scores_string; ?></span><br />

    <label>Score Total:</label>
    <span><?php echo $score_total; ?></span><br />

    <label>Average Score:</label>
    <span><?php echo $score_average; ?></span><br />
</form>

PHP:

if (isset($_POST['action'])) {
$action =  $_POST['action'];
} else {
$action =  'start_app';
}

switch ($action) {
case 'start_app':
    $scores = array();
    $scores[0] = 70;
    $scores[1] = 80;
    $scores[2] = 90;
    break;
case 'process_scores':
    $scores = $_POST['scores'];


    // validate the scores
    for ($i = 0; $i < count($scores); $i++) {
        if (empty($scores[$i]) || !is_numeric($scores[$i])) {
        $scores_string = 'You must enter three valid numbers for scores.';
        break;
        }
    }


    // process the scores
    $scores_string = '';
    foreach ($scores as $s) {
        $scores_string .= $s . '|';
    }
    $scores_string = substr($scores_string, 0, strlen($scores_string)-1);

    // Radio buttons
    $calculate_type = $_POST['calculate'];

    switch ($calculate_type) {
        case 'average':
            $score_tally = $scores[0] + $scores[1] + $scores[2];
            $score_average = $score_tally / count($scores);
            $score_average = number_format($score_average, 2);
            break;
        case 'total':
            $score_total = $scores[0] + $scores[1] + $scores[2];
            $score_total = number_format($score_total, 2);              
            break;
        case 'both':
            $score_tally = $scores[0] + $scores[1] + $scores[2];
            $score_average = $score_tally / count($scores);
            $score_total = $scores[0] + $scores[1] + $scores[2];
            $score_total = number_format($score_total, 2);
            $score_average = number_format($score_average, 2);
            break;
    }

    break;
case 'process_rolls':
    $number_to_roll = $_POST['number_to_roll'];

    $total = 0;
    // $count = 0;
    $max_rolls = -INF;

    for ($count = 0; $count < 1000; $count++) {
        $rolls = 1;
        while (mt_rand(1, 6) != 6) {
            $rolls++;
        }
        $total += $rolls;
        $max_rolls = max($rolls, $max_rolls);
    }
    $average_rolls = $total / $count;

    break;


}

使用此 for 循环验证分数,当存在无效数据时,我不会产生任何结果。

4

4 回答 4

2

由于您要求for循环:

for ($i = 0; $i < count($scores); $i++)
    if (empty($scores[$i]) || !is_numeric($scores[$i])) {
        $scores_string = 'You must enter three valid numbers for scores.';
        break;
    }
于 2013-10-06T00:14:34.383 回答
2

我会看一下array_reduce,因为这基本上就是您正在做的事情:将数组简化为布尔值。

if( array_reduce(
   $scores,
   function($a,$b) {return $a || empty($b) || !is_numeric($b);},
   false)) {
       $scores_string = "You must enter three valid numbers for scores.";
}
于 2013-10-06T00:08:11.557 回答
1

我不明白你为什么要测试分数是否为空。如果 score 为空,则is_numeric返回 false。

我的for循环版本:

for ($i = 0, $len = count($scores); $i < $len and is_numeric($scores[$i]); $i++) {}
if ($i !== $len) echo 'You must enter three valid numbers for scores.';

编辑:如果您需要测试数组中是否正好有 3 个数字项(如@geomagas 建议的那样),那么:

for ($i = 0; $i < 3 and is_numeric($scores[$i]); $i++) {}

// If $i is less then 3 it's mean that one of items are not set or it's not numeric
if ($i < 3) echo 'You must enter three valid numbers for scores.';

编辑:处理数组未正确索引时的情况的第三个解决方案。我发布这个只是为了好玩,拒绝使用empty@geomagas 强加给我的功能;)

$scores = array('234', '52', '245');

for (reset($scores); $valid = is_numeric(current($scores)) and next($scores);) {}

if ( ! $valid) echo 'You must enter three valid number for scores.';
于 2013-10-06T00:37:53.933 回答
0

empty()不应使用,因为0它被认为是空的并且是数字的。

$size = count($scores);
for ($i = 0; $i < $size; $i++) {
    if (!is_numeric($scores[$i])) {
        $scores_string = 'You must enter three valid numbers for scores.';
        break;
    }
}
于 2013-10-06T00:54:53.340 回答