默认情况下如何使check循环结果为false

时间:2015-01-27 10:34:32

标签: php

我要求将此循环设置为默认值以允许其他功能进行播放。默认情况下如何将所有检查设置为false?

示例:默认情况下,将所有这些检查($ checkGeneral)设置为== 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;
    }
}

1 个答案:

答案 0 :(得分:1)

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;
    }
}