Php 如何使检查循环结果默认为false
我需要将此循环设置为默认值,以允许另一个功能发挥作用。如何在默认情况下将所有检查设置为false 示例:默认情况下,将下面的所有检查($checkGeneral)设置为Php 如何使检查循环结果默认为false,php,Php,我需要将此循环设置为默认值,以允许另一个功能发挥作用。如何在默认情况下将所有检查设置为false 示例:默认情况下,将下面的所有检查($checkGeneral)设置为==false $resultCatAdd = $catCheckA; $catCheckA = explode(",", $stockrm['einv_stockrm_cat']); foreach($catCheckA as &$value) { if($value == "General Informatio
==false
$resultCatAdd = $catCheckA;
$catCheckA = explode(",", $stockrm['einv_stockrm_cat']);
foreach($catCheckA as &$value) {
if($value == "General Information") {
$checkGeneral = true;
} elseif ($value == "Product Information") {
$checkProduct = true;
} elseif ($value == "Warranty Information") {
$checkWarranty = true;
} elseif ($value == "Sales Parts") {
$checkSales = true;
} elseif ($value == "Customer Information") {
$checkCustomer = true;
} elseif ($value == "Internal Information") {
$checkInternal = true;
} elseif ($value == "Warehouse Information") {
$checkWarehouse = true;
} elseif ($value == "Yearly Reset") {
$checkYR = true;
}
}
您可以根据字符串生成变量名,然后将其设置为false,如下所示:
您的逻辑应该比您认为的解决方案更简单,imho(例如,您可以去掉所有这些标志)。从一开始就重新考虑:)开关
和案例
比if
和else if
更好,您可以根据自己的要求定义一个默认
案例
You could make variable names according strings and then set them to false as below:
<?php
$stockrm['einv_stockrm_cat'] = "General Information,Product Information,Warranty Information,Sales Parts,Customer Information,Internal Information,Warehouse Information,Yearly Reset";
$catCheckA = explode(",", $stockrm['einv_stockrm_cat']);
foreach($catCheckA as $key => $value) {
$varName = explode(" ", $value);
$check{ucfirst($varName[0])} = false;
}
foreach($catCheckA as &$value) {
if($value == "General Information") {
$checkGeneral = true;
} elseif ($value == "Product Information") {
$checkProduct = true;
} elseif ($value == "Warranty Information") {
$checkWarranty = true;
} elseif ($value == "Sales Parts") {
$checkSales = true;
} elseif ($value == "Customer Information") {
$checkCustomer = true;
} elseif ($value == "Internal Information") {
$checkInternal = true;
} elseif ($value == "Warehouse Information") {
$checkWarehouse = true;
} elseif ($value == "Yearly Reset") {
$checkYearly = true;
}
}