PHP question...I posted this on the Web Programming forum, but maybe this is a better place!
I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names.
So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc
So what I want to do is display the $col value but the person's name, not the id...
ie
$person[$col[1]] should display "Fred"
$person[$col[3]] should display "Tom"
etc
However, this doesn't want to parse correctly and I get errors, with or without quotes. I've verified that $col[1] is 1 and $person[1] is "Fred", for example. What syntax is required?
Any help appreciated.
Jerry
Hi,
use eval.
If col=(1 7 3) and person=(fred bert tom)
This:
echo ${person[$(eval echo ${col[3]})]}
will print out: tom
and
echo ${person[$(eval echo ${col[1]})]}
will give you: fred
HTH Chris
Thanks Chris, but your fix didn't want to work either! I've just discovered that my original format works fine as long as it's not embedded as a variable in a print quoted string, so maybe Chris's fix suffers from the same problem.
So this is broken...
print "First person is $person[$col[1]]";
...but this works...
print "First person is " . $person[$col[1]];
the double quotes will replace the variable by it's value. This will work fine for "Hello $world". As soon as there are many worlds and you refer to one by "Hello $world[0]" PHP will try to interprete the variable name. Since [ is not a valid char for a variable name it will interpret the variable name as $world only which is not set and therefore nothing is displayed.