如何找到字符串变量并与数组进行比较?
How to find string variable and compare with array?
我有这个数组:
Array
(
[0] =>
bool own = 0
[1] =>
bool contr_name = 0
[2] =>
int all_votes = 0
[3] =>
bool contract_start = 0
[4] =>
bool contract_end = 0
[11] =>
clock T
[12] =>
int a
[13] =>
int candi_ID = 1
[14] =>
int voter_ID = 1
[15] =>
)
首先我想保存数据类型、变量名和值。
其次,我想比较一个变量,就像我在数组中是否有这个变量,然后找到它是否相等的值。这是我的代码:
$variable = "own=1";
function searchTheValue($afterExp,$variable){
$datatype ="";
$variablename ="";
$equalto ="";
$varaiablevalue ="";
foreach ($afterExp as $newtest){
$afterExpBySpace = explode(" ",$newtest);
if (isset($afterExpBySpace[0])){
$datatype = !empty($afterExpBySpace[0]) ? $afterExpBySpace[0] : "";
}
if (isset($afterExpBySpace[1])){
$variablename = !empty($afterExpBySpace[1]) ? $afterExpBySpace[1] : "";
}
if (isset($afterExpBySpace[2])){
$equalto = !empty($afterExpBySpace[2]) ? $afterExpBySpace[2] : "";
}
if (isset($afterExpBySpace[2])){
if (!empty($afterExpBySpace[3])){
$varaiablevalue = $afterExpBySpace[3];
}else{
if($afterExpBySpace[3] == "0"){
$varaiablevalue = "0";
}
}
}
echo $datatype."datatype<br>" ;
echo $variablename."variable name<br>" ;
echo $equalto." = <br>";
echo $varaiablevalue." variable value";exit;
}
}
searchTheValue($afterExp,$variable);
所以,这里我有 $variable="own=1";。我想在数组中搜索变量名是否存在于数组中的变量,并比较它是否等于 1 的值。任何建议将不胜感激。
就像您拥有的更清晰的版本一样,还添加了检查您正在寻找的变量的部分。
此代码首先处理值数组,这意味着这些值可以反复使用(更新等)而无需多次转换它们,然后 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]);
if ( count($nameAndType) > 1) {
// 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;
}
只是为了显示 $variables
数据(部分)...
Array
(
[own] => Array
(
[type] => bool
[value] => 0
)
[contr_name] => Array
(
[type] => bool
[value] => 0
)
[all_votes] => Array
(
[type] => int
[value] => 0
)
我有这个数组:
Array
(
[0] =>
bool own = 0
[1] =>
bool contr_name = 0
[2] =>
int all_votes = 0
[3] =>
bool contract_start = 0
[4] =>
bool contract_end = 0
[11] =>
clock T
[12] =>
int a
[13] =>
int candi_ID = 1
[14] =>
int voter_ID = 1
[15] =>
)
首先我想保存数据类型、变量名和值。 其次,我想比较一个变量,就像我在数组中是否有这个变量,然后找到它是否相等的值。这是我的代码:
$variable = "own=1";
function searchTheValue($afterExp,$variable){
$datatype ="";
$variablename ="";
$equalto ="";
$varaiablevalue ="";
foreach ($afterExp as $newtest){
$afterExpBySpace = explode(" ",$newtest);
if (isset($afterExpBySpace[0])){
$datatype = !empty($afterExpBySpace[0]) ? $afterExpBySpace[0] : "";
}
if (isset($afterExpBySpace[1])){
$variablename = !empty($afterExpBySpace[1]) ? $afterExpBySpace[1] : "";
}
if (isset($afterExpBySpace[2])){
$equalto = !empty($afterExpBySpace[2]) ? $afterExpBySpace[2] : "";
}
if (isset($afterExpBySpace[2])){
if (!empty($afterExpBySpace[3])){
$varaiablevalue = $afterExpBySpace[3];
}else{
if($afterExpBySpace[3] == "0"){
$varaiablevalue = "0";
}
}
}
echo $datatype."datatype<br>" ;
echo $variablename."variable name<br>" ;
echo $equalto." = <br>";
echo $varaiablevalue." variable value";exit;
}
}
searchTheValue($afterExp,$variable);
所以,这里我有 $variable="own=1";。我想在数组中搜索变量名是否存在于数组中的变量,并比较它是否等于 1 的值。任何建议将不胜感激。
就像您拥有的更清晰的版本一样,还添加了检查您正在寻找的变量的部分。
此代码首先处理值数组,这意味着这些值可以反复使用(更新等)而无需多次转换它们,然后 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]);
if ( count($nameAndType) > 1) {
// 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;
}
只是为了显示 $variables
数据(部分)...
Array
(
[own] => Array
(
[type] => bool
[value] => 0
)
[contr_name] => Array
(
[type] => bool
[value] => 0
)
[all_votes] => Array
(
[type] => int
[value] => 0
)