I have the following code that works:
$apple_color = "red";
$pear_color = "green";
$banana_color = "yellow";
$grape_color = "purple";
foreach(array("apple","pear","banana","grape") as $idx=>$fname) {
$var = "${fname}_color";
echo "$var = ${$var}<br>".PHP_EOL;
}
However, I cannot figure out how to shorten the code by eliminating the $var variable to something like this:
foreach(array("apple","pear","banana","grape") as $idx=>$fname) {
echo "${fname}_color = ${${fname}_color}<br>".PHP_EOL;
}
The "${${fname}_color" returns an PHP error that says "syntax error, unexpected '_color' (T_STRING)"
PHP Fiddle: http://phpfiddle.org/lite/code/wrfp-yqkx