# PHP arrays as array elements

**URL:** <https://community.unix.com/t/php-arrays-as-array-elements/225832>\
**Category:** Shell Programming and Scripting\
**Created:** [December 28, 2008, 7:58am UTC](https://community.unix.com/t/php-arrays-as-array-elements/225832 "2008-12-28T07:58:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JerryHone](https://community.unix.com/letter_avatar/jerryhone/32/6_449c533f586939b6e5f246632a46421f.png) [@JerryHone](https://community.unix.com/u/JerryHone)\
**Post date:** [December 28, 2008, 7:58am UTC](https://community.unix.com/t/php-arrays-as-array-elements/225832/1 "2008-12-28T07:58:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Christoph\_Spohr](https://community.unix.com/letter_avatar/christoph_spohr/32/6_449c533f586939b6e5f246632a46421f.png) [@Christoph\_Spohr](https://community.unix.com/u/Christoph_Spohr)\
**Post date:** [December 28, 2008, 8:27am UTC](https://community.unix.com/t/php-arrays-as-array-elements/225832/2 "2008-12-28T08:27:38Z")

</div>

Hi,

use eval.

If col=(1 7 3) and person=(fred bert tom)

This:

```nohighlight
echo ${person[$(eval echo ${col[3]})]}

```

will print out: tom

and

```nohighlight
echo ${person[$(eval echo ${col[1]})]}

```

will give you: fred

HTH Chris

---

<div class="post-metadata">

**Author:** ![JerryHone](https://community.unix.com/letter_avatar/jerryhone/32/6_449c533f586939b6e5f246632a46421f.png) [@JerryHone](https://community.unix.com/u/JerryHone)\
**Post date:** [December 28, 2008, 1:01pm UTC](https://community.unix.com/t/php-arrays-as-array-elements/225832/3 "2008-12-28T13:01:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![strobotta](https://community.unix.com/letter_avatar/strobotta/32/6_449c533f586939b6e5f246632a46421f.png) [@strobotta](https://community.unix.com/u/strobotta)\
**Post date:** [December 29, 2008, 4:13am UTC](https://community.unix.com/t/php-arrays-as-array-elements/225832/4 "2008-12-29T04:13:37Z")

</div>

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.
