如何使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和CCD_ 5:http://php.net/manual/en/function.array-push.php

您的逻辑有缺陷-您在$cnt数组上循环,并不断设置/替换禁用的/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]

您的内部循环是破坏性的——您只将LAST项的比较测试结果保存在$cnt中,因此您的"早期"测试结果会被破坏。

你应该拥有的是:

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

这应该适用于您:

(在这里,我用array_map()遍历$time的每个元素,并检查它是否在$cntin_array()的数组中。如果它在数组中,我会用替换来替换它,其中我使用use()从父作用域继承它,如果不在,我会再次分配值)

<?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 )

很多答案都显示了实现这一点的方法,但也有功能方法:

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

您可能想尝试以下操作,它不是最佳的,但它在使代码工作的同时更改了最少的代码量:

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

在_array:中使用PHP

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

只需添加一个"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];
                }   
            }
        }