简体   繁体   中英

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";. 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. Any suggestion will be appreciable.

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)...

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

}

Just to show the $variables data (partially)...

Array
(
    [own] => Array
        (
            [type] => bool
            [value] => 0
        )

    [contr_name] => Array
        (
            [type] => bool
            [value] => 0
        )

    [all_votes] => Array
        (
            [type] => int
            [value] => 0
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM