PHP arrays as array elements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PHP arrays as array elements
# 1  
Old 12-28-2008
PHP arrays as array elements

PHP question...I posted this on the Web Development 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
# 2  
Old 12-28-2008
Hi,

use eval.

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

This:
Code:
echo ${person[$(eval echo ${col[3]})]}

will print out: tom

and

Code:
echo ${person[$(eval echo ${col[1]})]}

will give you: fred

HTH Chris
# 3  
Old 12-28-2008
Fixed

Thanks Chris, but your fix didn't want to work either!Smilie 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]];
# 4  
Old 12-29-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiply elements of 2 arrays together into another array

So I need to Write an array processing program using a Linux shell programming language to perform the following. Load array X of 20 numbers from an input file X. Load array Y of 20 numbers from an input file Y. Compute array Z by multiply Xi * Yi then compute the square-root of this... (2 Replies)
Discussion started by: sarapham409
2 Replies

2. Shell Programming and Scripting

Sum elements of 2 arrays excluding labels

I'm looking for an efficient way to sum elements from 2 arrays using AWK and preserve header as well as sample names in the output array. I have Ubuntu 16.04 LTS. For example; ARRAY 1 SAMPLE DERIVED ANCESTRAL Sample1 14352 0 Sample2 14352 0 Sample3 14352 0 Sample4 ... (8 Replies)
Discussion started by: Geneanalyst
8 Replies

3. Shell Programming and Scripting

Compare multiple arrays elements using awk

I need your help to discover missing elements for each box. In theory each box should have 4 items: ITEM01, ITEM02, ITEM08, and ITEM10. Some boxes either have a missing item (BOX02 ITEM08) or might have da duplicate item (BOX03 ITEM02) and missing another one (BOX03 ITEM01). file01.txt ... (2 Replies)
Discussion started by: alex2005
2 Replies

4. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

5. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

6. Shell Programming and Scripting

Multiplication of array elements

Hi, I can't find out how to create correct code to get multiplication of each elements of array. Let's say I enter array into command line (2 3 4 5 6 8) and i need output 2*3*4*5*6*8=5760. I tried this one, but answer is 0. for i in $@; do mult=$((mult*i))done echo "mult: " $mult ... (4 Replies)
Discussion started by: rimasbimas
4 Replies

7. UNIX for Dummies Questions & Answers

printing array elements

Is there a way to print multiple array elements without iterating through the array using bash? Can you do something like... echo ${array}and get all those separate elements from the array? (2 Replies)
Discussion started by: jrymer
2 Replies

8. Shell Programming and Scripting

PHP: how can I delete empty/NULL elements from a multi-dimensional array.

Hi all I have a file that i'm running and exec(cat ./dat) against..and putting its contents into any array, then doing an exploding the array into a multi-dimension array... The 15 multi-dimensional arrays have elements that are null/empty, I would like to remove/unset these elements and then... (2 Replies)
Discussion started by: zeekblack
2 Replies

9. Shell Programming and Scripting

How to access the elements of two arrays with a single loop using the inbuilt index.

Hi all, I wanted to access two arrays (of same size) using one for loop. Ex: #!/bin/bash declare -a num declare -a words num=(1 2 3 4 5 6 7) words=(one two three four five six seven) for num in ${num} do echo ":$num: :${words}:" done Required Output: :1: :one: (11 Replies)
Discussion started by: 14341
11 Replies

10. UNIX for Dummies Questions & Answers

Deleting Array Elements

Hi, I am writing a BASH shell script. I have an array that will contain IN ANY ORDER the following elements: DAY 8D MO NS. I would like to erase the element DAY, but since the order of the elements in the array are random, I will not know which element # DAY is (ie it's not as simple as... (3 Replies)
Discussion started by: msb65
3 Replies
Login or Register to Ask a Question