0

I'm building a condition check where I'm passing in the variable and a value to check. In this case the variable is an array value, but I can't get it to return it correctly

//happens somewhere else
$specialFeature['option1']="on";
$specialFeature['option2']="on";
$specialFeature['option3']="off";

//what I'm trying to do
#query a db
$row = $result->fetch_array()
#results for purpose of demo
#$row['var'] = "specialFeature['option2']";
#$row['val'] = "on";
if($$row['var'] == $row['val']){
    //what i'm expecting
    echo "OK";
}

My issue is $$var is always null. What am I doing wrong? Is this possible?

KPK
  • 442
  • 1
  • 5
  • 15

6 Answers6

1
//happens somewhere else
$ARRAY['myKey1']=1;
$ARRAY['myKey2']=2;
$ARRAY['myKey3']=3;

//what I'm trying to do
$var = "ARRAY['myKey3']";
$val = 3;
if(${$var} == $val){
    //what i'm expecting
    echo "OK";
}

From PHP.net: In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

Edit: The array-index seems to be the problem.

To split the variable name you could use something like

preg_match('/(.*)\[\'(.*)\'\]/', $row['var'], $matches);
RST
  • 3,899
  • 2
  • 20
  • 33
1

With $$var you get following: ${"ARRAY['myKey3']"}, so it is treated as a variable with name ARRAY['myKey3'] (Which doesn't exist, though you can create it with $$var = 'new value', but it will differ from $ARRAY['myKey3'] as they will be 2 different variables). Probably you are looking for if ($ARRAY['myKey3'] == $val) ?

Zudwa
  • 1,360
  • 10
  • 13
1
$ARRAY['myKey1']=1;
$ARRAY['myKey2']=2;
$ARRAY['myKey3']=3;

//what I'm trying to do
$var = "ARRAY";//pass variable name or array name
$val = 3;
if((${$var}['myKey3']) == $val){
    //what i'm expecting
    echo "OK";
}
user3510665
  • 218
  • 3
  • 13
0

Try this:

if(${$var} == $val){
    //what i'm expecting
    echo "OK";
}
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Mysteryos
  • 5,581
  • 2
  • 32
  • 52
0

Is that what you are looking for?

    $var = $ARRAY['myKey3'];
    $val = 3;
     if($var == $val){
     //what i'm expecting
     echo "OK";
           }
monirz
  • 534
  • 5
  • 14
  • And yes you are doing something wrong. You are assigning a value to the condition block, $$ for assigning a value that is equal to original variable but condition is just for checking the value its true or false, for in this instance $$var is NULL so the condition is not true that's why its returning nothing. Just see what happened with this code var_dump($$var); – monirz Dec 11 '14 at 06:47
0

I'm not sure if what I'm doing is "correct", but I got the result I was looking for by doing this:

//happens somewhere else
$specialFeature['option1']="on";
$specialFeature['option2']="on";
$specialFeature['option3']="off";

//what I'm trying to do
#query a db
$row = $result->fetch_array()
#results for purpose of demo
#$row['var'] = "specialFeature['option2']";
#$row['val'] = "on";

$var = $row['var'];

if (strpos($var,'[') !== false) {
    $varA = str_split($var,strpos($var,'['));
    $varA[1] = substr($varA[1],1,-1);
    if (strpos($varA[1],"'") !== false) {
        $varA[1] = substr($varA[1],1,-1);
    }
}

if(${$varA[0]}[$varA[1]] == $row['val']){
    //what i'm expecting
    echo "OK";
}
KPK
  • 442
  • 1
  • 5
  • 15