如何通过for循环中的if语句向数组添加单词?

问题描述 投票:0回答:2

是否可能,并且通过执行以下操作:

假设您在for循环中有一个if语句。每当该if语句的第一个条件为true时,就将相同的单词添加到数组中。

类似这样的东西:

for($i=1; $i<=$5; $i++){
if($name[$i-1] == "voornaam $i"){

  ///adds the word 'correct' to the array $successlist /// ;

} else{

  ///adds the word 'incorrect' to the array $successlist /// ;

} }

如果一切正常,则数组应如下所示:

$成功列表(正确,正确,正确,正确,正确等)

提前感谢。

php for-loop if-statement
2个回答
0
投票
if(condition){
    $successList[] = 'correct';
}else{
    $successList[] = 'incorrect';
}

0
投票
// Not $5, but `5` and start from 0:
for ($i=0; $i<5; $i++) {
    if($name[$i] == "voornaam $i"){
        $successlist[] = 'correct';
    } else{
        $successlist[] = 'incorrect';
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.