php 结构与 foreach into if
php structure with foreach into if
我需要创建一个结构,在检查 (1) 如果数组不为空后,我 (2) 检查数组中的第一个值是否大于 my_value,如果不是则检查数组的第二个值等等。
当 (2) 为真时 运行 我的指令,然后我需要退出循环并转到脚本末尾。
如果检查完我的数组 (2) 中的所有值都不是真的,那么我需要 return 到 (1)'else'
'end of script' 对所有人都是一样的,并且包含根据循环改变的变量
我尝试了 break 和 goto 但没有用。
正确的结构是什么。
这个结构正确吗?
$myvalue='value';
cart_mem_rest = array('value1','value2','value3','value4');
if( sizeof( $cart_mem_rest) != 0 ){ //condition 1
foreach($cart_mem_rest as $kmem => $vmem_rest){
if ($vmem_rest > $myvalue){
//condition 2 : if first array value is bigger of $myvalue
//run my instruction and go to end script, if not then check
//other value , and other value .....
my instruction
Break;//go to end script
}
}
}else{
//here if conditions 1 or 2 are not verificated
if(condition is true ){
my instruction
}else{
my instruction
}
}
//end script
echo ' end script';
我认为如果在 foreach 中比较成功,设置一个变量来保存结果可能会更容易。
在 foreach 中,您可以进行比较 if ($vmem_rest > $myvalue)
,当比较为真时,例如设置一个变量 $found = true
并跳出循环。
那你就不用检查数组不为空了,直接从foreach开始。如果数组为空,则foreach 没有任何处理。
现在我们可以检查循环中是否存在匹配项,检查 $found
的值
$myvalue = 'value';
$cart_mem_rest = array('value1', 'value2', 'value3', 'value4');
$found = false;
foreach ($cart_mem_rest as $kmem => $vmem_rest) {
if ($vmem_rest > $myvalue) {
// my instruction
$found = true;
break;
}
}
if (!$found) {
// my instruction
} else {
// my instruction
}
//end script
echo PHP_EOL . 'end script';
我需要创建一个结构,在检查 (1) 如果数组不为空后,我 (2) 检查数组中的第一个值是否大于 my_value,如果不是则检查数组的第二个值等等。 当 (2) 为真时 运行 我的指令,然后我需要退出循环并转到脚本末尾。
如果检查完我的数组 (2) 中的所有值都不是真的,那么我需要 return 到 (1)'else'
'end of script' 对所有人都是一样的,并且包含根据循环改变的变量
我尝试了 break 和 goto 但没有用。
正确的结构是什么。
这个结构正确吗?
$myvalue='value';
cart_mem_rest = array('value1','value2','value3','value4');
if( sizeof( $cart_mem_rest) != 0 ){ //condition 1
foreach($cart_mem_rest as $kmem => $vmem_rest){
if ($vmem_rest > $myvalue){
//condition 2 : if first array value is bigger of $myvalue
//run my instruction and go to end script, if not then check
//other value , and other value .....
my instruction
Break;//go to end script
}
}
}else{
//here if conditions 1 or 2 are not verificated
if(condition is true ){
my instruction
}else{
my instruction
}
}
//end script
echo ' end script';
我认为如果在 foreach 中比较成功,设置一个变量来保存结果可能会更容易。
在 foreach 中,您可以进行比较 if ($vmem_rest > $myvalue)
,当比较为真时,例如设置一个变量 $found = true
并跳出循环。
那你就不用检查数组不为空了,直接从foreach开始。如果数组为空,则foreach 没有任何处理。
现在我们可以检查循环中是否存在匹配项,检查 $found
$myvalue = 'value';
$cart_mem_rest = array('value1', 'value2', 'value3', 'value4');
$found = false;
foreach ($cart_mem_rest as $kmem => $vmem_rest) {
if ($vmem_rest > $myvalue) {
// my instruction
$found = true;
break;
}
}
if (!$found) {
// my instruction
} else {
// my instruction
}
//end script
echo PHP_EOL . 'end script';