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?

2
  • why not you try like this simply call as $var = "$ARRAY['myKey3']"; Commented Dec 11, 2014 at 6:28
  • $val == $ARRAY['myKey3'] Commented Dec 11, 2014 at 6:30

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explanation. This put me on the right path.
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";
}

Comments

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

Comments

0

Is that what you are looking for?

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

1 Comment

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);
0

Try this:

if(${$var} == $val){
    //what i'm expecting
    echo "OK";
}

1 Comment

Tried that with no success
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";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.