1

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.

Abbas
  • 25
  • 4

1 Answers1

0

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
        )
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Thanks a lot @Nigel Ren. I got the idea now but still I am getting this error: Notice: Undefined offset: 1 in: This is the line: $variables[ trim($nameAndType[1])] = ["type" => trim($nameAndType[0]), "value" => isset($splitEquals[1]) ? trim($splitEquals[1]): NULL ]; – Abbas Dec 04 '19 at 05:26
  • Do you get this with the code I show, or do you have different values in `$afterExp`? – Nigel Ren Dec 04 '19 at 07:13
  • yeah it's same if you can check we have the last array empty or just a space maybe it's just because if that array. – Abbas Dec 04 '19 at 07:25
  • I've added a check that you need 2 parts to store the variable. Not sure what you need to do with the ones that aren't properly formed though. – Nigel Ren Dec 04 '19 at 07:31
  • i tried array_filter($afterExp); but it's not gonna work. Still getting the last empty array. – Abbas Dec 04 '19 at 07:32
  • with ones that aren't properly formed like we have int a so we don't need to compare the value we will just assign the value of int a. like if we have $variable = "a=1"; so we will check if "a" exists in $variables and then we will assign "1" to that variable. – Abbas Dec 05 '19 at 04:39