Just as cleaner version of what you have and also adding in the part which checks the variable you are looking for.
(就像您拥有的内容的更干净版本一样,还添加了检查所需变量的部分。)
This code first processes the array of values, this means that these values can be used over and over (updated etc.) without have to convert them several times, then the searchTheValue()
function just looks for the value you pass in (comments in code for clarity)...
(此代码首先处理值的数组,这意味着可以反复使用(更新等)这些值,而不必多次转换它们,然后searchTheValue()
函数仅查找您传入的值(注释代码))
$afterExp = ["bool own = 0","bool contr_name = 0",
"int all_votes = 0","bool contract_start = 0",
"bool contract_end = 0","clock T","int a",
"int candi_ID = 1","int voter_ID = 1",""];
$variables = [];
foreach ($afterExp as $variable){
if ( !empty($variable) ) {
// Split type and name from value
$splitEquals = explode("=", $variable);
// Split type and name
$nameAndType = explode(" ", $splitEquals[0]);
// Set variables with name, type and value (defaulter to null if not set)
$variables[ trim($nameAndType[1])] = ["type" => trim($nameAndType[0]),
"value" => isset($splitEquals[1]) ? trim($splitEquals[1]): NULL
];
}
}
$variable = "own=0";
echo searchTheValue($variables,$variable);
function searchTheValue($variables,$variable) {
// Split variable looking for into name and value
$var = explode("=", $variable);
$match = false;
// If this variable is set
if ( isset($variables [trim($var[0])]) ) {
// Compare value with stored value
if ( $variables [trim($var[0])]['value'] == trim($var[1]) ) {
$match = true;
}
}
// Note that if the variables isn't found, it will also just return false
return $match;
}
Just to show the $variables
data (partially)...
(只是为了显示$variables
数据(部分)...)
Array
(
[own] => Array
(
[type] => bool
[value] => 0
)
[contr_name] => Array
(
[type] => bool
[value] => 0
)
[all_votes] => Array
(
[type] => int
[value] => 0
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…