簡體   English   中英

如何找到字符串變量並與數組進行比較?

[英]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
        )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM