Rob Williams
Rob Williams

Reputation: 37

How do I turn a string reference into a variable

I want $masking_x to act as $masking_y (i.e. dynamically insert $radiovalue but still act as a varibale).

My code is

while($field_radio = mysql_fetch_assoc($result_radio)) {
    $radiovalue = $field_radio[radiovalue];
    echo "Radio value: ".$radiovalue."</br>";   
    $masking = "field_masking[checkbox_1001_".$radiovalue."]";
    $masking_x = '$'.$masking;
    $masking_y = "$field_masking[checkbox_1001_2]";
    echo "Masking: ".$masking."</br>";
    echo "Masking_x: ".$masking_x."</br>";
    echo "Masking_y: ".$masking_y."</br>";
    die;

I'm getting the following output:

Radio value: 2
Masking: field_masking[checkbox_1001_2]
Masking_x: $field_masking[checkbox_1001_2]
Masking_y: 2

Any guidance would be greatly appreciated. Thanks in advance.

Upvotes: 1

Views: 97

Answers (3)

Naltharial
Naltharial

Reputation: 2152

If you want to dynamically access an array variable, split it in two parts, single variable won't work.

$name = 'field_masking';
$key = 'checkbox_1001_'.$radiovalue;

echo $$name[$key];

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88647

You are using variable variables slightly wrong. Try this instead:

$radiovalue = $field_radio['radiovalue'];
echo "Radio value: ".$radiovalue."</br>";   
$masking_x = "field_masking['checkbox_1001_".$radiovalue."']";
$masking_y = "field_masking['checkbox_1001_2']";
echo "Masking_x: ".$$masking_x."</br>";
echo "Masking_y: ".$$masking_y."</br>";

The double dollar-sign must be in the code, not in the string.

It's also possible to do this:

$radiovalue = $field_radio['radiovalue'];
echo "Radio value: ".$radiovalue."</br>";   
echo "Masking_x: ".${"field_masking['checkbox_1001_".$radiovalue."']"}."</br>";
echo "Masking_y: ".${"field_masking['checkbox_1001_2']"}."</br>";

Note also that I added single quotes into your string to properly quote the strings used for the array keys, associative array keys these are regular strings and should be quoted as such.

However it is important to note that variable variables are rarely the right solution to a given problem. It's usually possible to come up with something better using arrays or references instead.

EDIT

After playing around with it, it seems you cannot use a variable variable to reference an array key when the key definition is in the string. It's such a horrible thing to do, I'm not surprised I've never come across this limitation.

The long of the short of it is that you cannot do exactly what you are trying to do, and the best solution for you is probably to use references instead:

$radiovalue = $field_radio['radiovalue'];
echo "Radio value: ".$radiovalue."</br>";   
$masking = &$field_masking['checkbox_1001_'.$radiovalue];
echo "Masking: ".$masking."</br>";
$masking = "A different value";
echo "Masking: ".$masking."</br>";
// Original value has been updated as well
echo "\$field_masking['checkbox_1001_'.\$radiovalue]: ".$field_masking['checkbox_1001_'.$radiovalue]."</br>";

Upvotes: 2

mogelbrod
mogelbrod

Reputation: 2306

Take a look at variable variables. Do note that this is a potential security risk if you're working with user input.

In your case: $masking_x = $$masking;

Upvotes: 0

Related Questions