检查条件并多次追加字符串

时间:2016-05-09 06:36:08

标签: php arrays concatenation

以下是分配给变量 $ value

的数组
Array
(
    [colname] => leadnumber
    [tabname] => lead
    [uitypeid] => 14
    [fieldname] => leadnumber
    [filedlabel] => Lead Number
    [readmode] => Yes
    [mandmode] => No
    [fieldmode] => Yes
    [fieldseq] => 1
    -----
    ----
)

我需要什么。 我必须检查每个项目的值,并添加如下。

$rule = "required|unique|read|tabname|lorem|ipsum|etc.."

我在下面做过。

if($value['mandmode'] == "No"){
    $rule = 'trim';
}
if($value['modfieldunique'] == "No"){
    $rule = $rule.'|unique';
}
if($value['readmode'] == "No"){
    $rule = $rule.'|read';
}

我的问题是我有很多这样的条件来检查每个值,然后再将它附加到变量。如果我这样做,脚本运行时间会增加。有没有更好的方法来获得该格式的结果?

4 个答案:

答案 0 :(得分:1)

简单地采用一系列规则。

根据条件为其附加值。

最后,我们将只获得必需的规则作为数组元素。

现在,只需使用<?php $rules = array(); if ($value['mandmode'] == "No"){ $rules[] = 'trim'; } if ($value['modfieldunique'] == "No"){ $rules[] = 'unique'; } if ($value['readmode'] == "No"){ $rules[] = 'read'; } $rule = ! empty($rules) ? implode('|', $rules) : ''; ?> 附加所有内容。

完成

<强>代码:

if

答案 1 :(得分:1)

您可以将规则结构化为数组,例如

$referenceRules = [
    "manmode"           => "trim",
    "modfieldunique"    => "unique",
    "readmode"          => "read"
];

然后迭代你的数组并检查哪些元素符合规则 如果当前值为“否”,则if子句检查当前密钥是否也是referenceRules数组and的密钥。
假设您正在检查所有元素以匹配“否”。

$rules = [];

foreach($array as $key => $value){

    if (in_array($key,array_keys($referenceRules)) and $value == "No"){

        print $key . ":" . $value . PHP_EOL;    
        $rules[] = $referenceRules[$key];

    }

}

使用此阵列

$array = [
    "colname" => "leadnumber",
    "tabname" => "lead",
    "uitypeid" => 14,
    "fieldname" => "leadnumber",
    "filedlabel" => "Lead Number",
    "readmode" => "No",
    "manmode" => "No",  // I changed the key here to match your rules
    "fieldmode" => "Yes",
    "fieldseq" => 1
];   

以上将输出

readmode:No
manmode:No

最后只需implode $rules数组输出您需要的字符串

$ruleString = implode("|",$rules);
print $ruleString;

这将输出

read|trim

答案 2 :(得分:1)

尝试以下循环:

$rule = '';
foreach($value as $elementKey => $elementValue)
{
    switch($elementKey)
    {
        case "mandmode":
            if($elementValue == 'no')
                $rule .= 'trim|';
        break;

        case "modfieldunique":
            if($elementValue == 'no')
                $rule .= 'unique|';
        break;

        case "readmode":
        case "XYZ":         //You can add more conditions as requirement
            if($elementValue == 'no')
                $rule .= 'read|';
        break;
    }
}

$rule = substr($rule,0,-1);

您可以添加多个可能出现相同条件的情况......希望这有帮助

答案 3 :(得分:1)

// Take arrays from AlexAndrei answer
$referenceRules = [
    "manmode"           => "trim",
    "modfieldunique"    => "unique",
    "readmode"          => "read"
];
$array = [
    "colname" => "leadnumber",
    "tabname" => "lead",
    "uitypeid" => 14,
    "fieldname" => "leadnumber",
    "filedlabel" => "Lead Number",
    "readmode" => "No",
    "manmode" => "No",  // I changed the key here to match your rules
    "fieldmode" => "Yes",
    "fieldseq" => 1
];   
// Save only those which equal No
$temp = array_filter($array, function($i) { return $i === "No"; });
// Take corresponding itemms from referenceRules array
$temp = array_intersect_key($referenceRules, $temp);
// Make string
echo implode('|', $temp); // trim|read

demo