Php 如果pass为value,如何插入数组

Php 如果pass为value,如何插入数组,php,Php,值:test1=通过,test2=失败,test3=通过 我要1美元,3美元 只带通行证 我想在数组中输入一个等于pass的值。条件是该值必须等于pass。 但是test1、test2、test3不是数组。您可能正在寻找以下内容: <?php $test1 ="pass"; $test2 = "fail"; $test3 = "pass"; $check = array(); for($i=0;$i<3;$i++){ if($test1== "

值:test1=通过,test2=失败,test3=通过

我要1美元,3美元

只带通行证


我想在数组中输入一个等于pass的值。条件是该值必须等于pass。
但是test1、test2、test3不是数组。

您可能正在寻找以下内容:

<?php
$test1 ="pass";
$test2 = "fail";
$test3 = "pass";
$check = array();
        for($i=0;$i<3;$i++){
            if($test1== "pass"){
                $num = 1;
            }
            if($test2 == "pass"){
                $num = 2;
            }
            if($test3 == "pass"){
                $num = 3;
            }

            echo $num;
            $check[$i] = $num;
        }
?>
这有点难说,但如果是这样的话,这将使用,在使用顺序变量时减少一些脚本。

您可以这样做:

Array
(
    [0] => 1
    [1] => 3
)

很难理解你想要实现什么。您希望如何在实际场景中使用此代码?不清楚,无法理解您的问题我想在数组中输入一个等于pass的值。条件是该值必须等于pass。请提供所需的输出。
Array
(
    [0] => 1
    [1] => 3
)
$test1 ="pass";
$test2 = "fail";
$test3 = "pass";
$check = array();
$num = '';

if($test1== "pass"){
    $num .= '1,';
}

if($test2 == "pass"){
    $num .= '2,';
}

if($test3 == "pass"){
    $num .= '3,';
}

$num = trim($num,',');
$check = explode(',',$num);
$test1 ="pass";
$test2 = "fail";
$test3 = "pass";    
$num =array();

for($i=0;$i<3;$i++){
    if($test1 == "pass"){
        $num[] = 1;                
    }

    if($test2 == "pass"){
        $num[] = 2;
    }

    if($test3 == "pass"){
        $num[] = 3;
    }
}

print_r(array_unique($num));