如何使PHP for循环有条件?

How to make PHP for loop with condition?

我正在使用 PHP 创建带条件的循环,但结果与我想要的不一样。

如果您能提供任何帮助,那就太好了! :)

$cnt = array(
    "08.00","09.00","10.00"
);
$time = array(
    "07.00","08.00","09.00","10.00","11.00","12.00","13.00","14.00",
    "15.00","16.00","17.00","18.00","19.00","20.00","21.00","22.00"
);

这是我的循环代码:

for ($i = 0; $i < 16 ; $i++) {
    for ($j = 0; $j < 3 ; $j++) {
        if ($time[$i] == $cnt[$j]) {
            $button[$i] = 'disable';
        } else {
            $button[$i] = $time[$i];
        }
    }
}

结果是:

07.00 08.00 09.00 disable 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 20.00 21.00 22.00

我想要的结果是:

07.00 disable disable disable 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 20.00 21.00 22.00

我会使用这样的东西:

$button = array();
foreach($time as $t) {
    if(in_array($t, $cnt, true)) {
        $button[]='disable';
    } else {
        $button[]=$t;
    }
}

循环遍历 $time 数组的所有元素并检查每个值是否数组 $cnt 包含相同的值。

由于您使用递增索引添加值,因此您可以使用数组推送运算符 []= 将值附加到 $button 数组。

了解有关 in_array() 的更多信息:http://php.net/manual/en/function.in-array.php and []=: http://php.net/manual/en/function.array-push.php

您可能想尝试以下方法,这不是最佳方法,但它在使代码正常工作时更改的代码量最少:

for ($i=0; $i < 16 ; $i++) { 
     if(in_array($time[$i], $cnt) {
          $button[$i]='disable';
     } else {
          $button[$i]=$time[$i];
     }
}

您的逻辑有缺陷 - 您在 $cnt 数组上循环并不断 set/replace 您的 disabled/time 值。考虑一下:

$time -> looking at 08:00

在 $cnt 上循环:

08:00 -> matches -> set $button[$i] to disabled
09:00 -> no match -> set $button[$i] to $time[$i]
10:00 -> no match -> set $button[$i] to $time[$i]

你的内部循环是破坏性的 - 你只保存了 $cnt 中最后一项的比较测试结果,因此你的 "earlier" 测试结果被破坏了。

你应该拥有的是:

foreach($time as $idx => $val) {
    if (in_array($val, $cnt)) {
        $button[$idx] = 'disabled');
    } else {
        $button[$idx] = $val;
    }
}

只需要在内部循环中添加一个'break;'。见下文,

        for ($i=0; $i < 16 ; $i++) { 
            for ($j=0; $j < 3 ; $j++) { 
                if($time[$i]==$cnt[$j]){
                    $button[$i]='disable';
                    break;
                }else{
                    $button[$i]=$time[$i];
                }   
            }
        }

使用PHP in_array:

for ($i=0; $i < 16 ; $i++) { 
    if(in_array( $time[$i], $cnt ){
        $button[$i]='disable';
    }else{
        $button[$i]=$time[$i];
    }   
}

很多答案显示了这样做的方法,但也有功能方法:

$button = array_map(
    function ($item) use ($cnt) {
        return in_array($item, $cnt) ? 'disable' : $item;
    },
    $time
);

这应该适合你:

(这里我用 array_map() and check if it is in the array of $cnt with in_array(). If it is in the array I replace it with the replacement where I use use() 遍历 $time 的每个元素,从父作用域继承它,如果没有,我再次赋值)

<?php

    $cnt = array("08.00", "09.00", "10.00");
    $time = array("07.00", "08.00", "09.00", "10.00", "11.00", "12.00", "13.00", "14.00", "15.00", "16.00", "17.00", "18.00", "19.00", "20.00", "21.00", "22.00");  
    $replacement = "disabled";

    $time = array_map(function ($v) use ($cnt, $replacement) {
        return (in_array($v, $cnt) ? $replacement : $v);
    }, $time);

    print_r($time);

?>

输出:

Array ( [0] => 07.00 [1] => disabled [2] => disabled [3] => disabled [4] => 11.00 [5] => 12.00 [6] => 13.00 [7] => 14.00 [8] => 15.00 [9] => 16.00 [10] => 17.00 [11] => 18.00 [12] => 19.00 [13] => 20.00 [14] => 21.00 [15] => 22.00 )