<?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;
}
?>
值:test1 =“pass”,test2 =“fail”,test3 =“pass”
我想:$ check(1,3)
只选择“通行证”
我想在数组中输入一个等于“pass”的值。条件是该值必须等于“通过”。 但test1,test2,test3不是数组。
答案 0 :(得分:0)
据推测,你正在寻找这个:
$test1 = "pass";
$test2 = "fail";
$test3 = "pass";
$check = array();
for($i = 1; $i <=3; $i++) {
$t = 'test'.$i;
if(${$t} == 'pass')
$check[] = $i;
}
print_r($check);
给你:
Array
(
[0] => 1
[1] => 3
)
这有点难以辨别,但如果是这样,则使用Variable Variables,在使用顺序变量时减少某些脚本。
答案 1 :(得分:0)
您可以这样做:
$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);
答案 2 :(得分:0)
$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));