Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

php - 如何找到字符串变量并与数组比较?(How to find string variable and compare with array?)

I Have this 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] => 

)

First of all I wanna save datatype, variable name and value.

(首先,我要保存数据类型,变量名称和值。)

Second I wanna compare a variable like I have this variable in array or not and then find the value it's equal or not.

(其次,我想比较一个变量,例如我是否在数组中有此变量,然后找到它是否相等。)

Here is my code:

(这是我的代码:)

$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);

So, here i have $variable="own=1";.

(因此,这里我有$ variable =“ own = 1”;。)

I wanna search the variable in array that variable name is exist in array or not and the compare the value that it's equal to 1 or not.

(我想在数组中搜索变量名是否存在于数组中,并比较它等于1或不等于1的值。)

Any suggestion will be appreciable.

(任何建议都是可取的。)

  ask by Abbas translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
        )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...