Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
why not you try like this simply call as $var = "$ARRAY['myKey3']"; –  Fresher Dec 11 '14 at 6:28
    
$val == $ARRAY['myKey3'] –  sectus Dec 11 '14 at 6:30

6 Answers 6

Try this:

if(${$var} == $val){
    //what i'm expecting
    echo "OK";
}
share|improve this answer
    
Tried that with no success –  KPK Dec 11 '14 at 6:57
//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);
share|improve this answer

Is that what you are looking for?

    $var = $ARRAY['myKey3'];
    $val = 3;
     if($var == $val){
     //what i'm expecting
     echo "OK";
           }
share|improve this answer
    
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); –  monir009 Dec 11 '14 at 6:47

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

share|improve this answer
    
Thanks for the explanation. This put me on the right path. –  KPK Dec 11 '14 at 7:21
$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";
}
share|improve this answer
up vote 0 down vote accepted

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";
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.