I'm confused in how to use $$ to use a string as a variable, mainly when it comes to use a string to refer an array index. Consider the following case:
$colors = array(
'r'=>"red",
'b'=>"blue"
);
$vr = "colors[r]"; //I tried even this "color['r']"
echo $$vr; // I tried even this ${$vr}
Can anyone tell if it is possible to do the above. expected o/p is red using "color[r]" as string and then using it as variable.
You can't do that directly. Consider the following:
$varName = array_shift(explode('[', $vr));
foreach($$varName as $key=>$value){
echo $key.": ".$value."<br />";
}
this will print out:
r: red
b: blue
The variable variable is just the first part (colors). You can't include the key in this.