Set variable with variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Set variable with variable
# 1  
Old 02-16-2010
Set variable with variable

Okay, probably an easy solution, but I've given up. I want to set a variable with a variable and print it in PHP. I just can't get the syntax right. As an example:

Code:
$place["t"] = "T";

$norm_t = array("4","7");

I want to use this in a for-loop. How do I echo the first number of the norm_t array???

I've tried:
Code:
foreach ($place as $id => $name)  {
#so id is now t.  

echo "$norm_{$id}[0]"  
}

# This doesn't seem to work. I want to echo the variable $norm_t using the "id" variable. I'm new to php and can think of how this is done in other languages, but the proper syntax for php eludes me.

Dave

Last edited by wxornot; 02-16-2010 at 03:55 PM.. Reason: reduced code
# 2  
Old 02-19-2010
I don't think you can construct the names of a variable in PHP by treating the variable name as a string and concatenating two variables names together as you are trying to do. One way to do it in PHP would be to define norm_t as a two dimensional array.

Note: I could be wrong, but I have not seen this (as you are trying to do) and I look at a lot of PHP code every week!

PS: You are missing a ";" in your code (syntax error)
# 3  
Old 02-19-2010
variables

I think your right. I've search through all sorts of online documentation, and have found nothing. No big deal. Time to find a better route. Smilie
# 4  
Old 02-19-2010
If you use a two dimensional array it is easy...
# 5  
Old 02-22-2010
two dimensional

I have not used two dimensional arrays before. I will look into it. thanks,

Dave
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

do you have a better way to set this variable?

greetings, i have a variable $input that i want to use to set $output. $input is /dir/filename.mph and $input is passed to my script that i manipulate it as follows: input=`basename $input`i want the $output to be filename_solved.mph, basically stuffing "_solved" in the filename. here's how i... (2 Replies)
Discussion started by: crimso
2 Replies

2. Shell Programming and Scripting

How to know who and where a variable is set ?

hi, i'm not a root user and i want to know which user and in which file is loaded a variable seen in the "env" display ? I will use this variable but i want to be sure that it will be a permanent variable ! i don't see it in my files (.profile , kshrc...) and neither in /etc/profile. ... (3 Replies)
Discussion started by: Nicol
3 Replies

3. HP-UX

What is the use of command set -- and set - variable?

Hi, I am using hp unix i want to know the use of the following commands set -- set - variable thanks (4 Replies)
Discussion started by: gomathi
4 Replies

4. UNIX for Dummies Questions & Answers

How to set a variable with a count variable i.e. VARIABLE$COUNT

Hi All I've very nearly finished this script I'm working on but have hit another idiots problem, after googling I can't see a solution for this one. I have a while count loop that checks checks two consecutive values then increments the count by two. What the script has to do is then check... (5 Replies)
Discussion started by: Bashingaway
5 Replies

5. Shell Programming and Scripting

set a variable with a variable name

Hi there, I can display a variable with a variable name: localhost:~# a=1 localhost:~# b=a localhost:~# echo $b a localhost:~# echo ${!b} 1 But how can I set a variable with a variable name? In the following example, I want to set a = 1: localhost:~# b=a localhost:~# $b=1 -bash:... (10 Replies)
Discussion started by: chebarbudo
10 Replies

6. Shell Programming and Scripting

set variable in while loop?

Dear All, Can anyone advise why this script isn't run as expected? =========================== status=0 cat /etc/passwd | while read line; do status=1 done echo $status =========================== it always return 0 , but not 1. why? anything wrong? Thanks. (1 Reply)
Discussion started by: tiger2000
1 Replies

7. Shell Programming and Scripting

global variable not being set

In ksh I thought a global variable was any variable in a script or function that did not have the typeset command. I have a global in my calling script which I increment in a function, but the value does not change in the calling script. Here is the code: function f_open_log { typeset -r... (5 Replies)
Discussion started by: robotball
5 Replies

8. Shell Programming and Scripting

set variable with another variable? c shell

okay, this shouldn't be difficult but I can't figure it out. How can I set a variable with another variable. I have the following: foreach pe ($dir $sp) set tpe = `echo $pe | grep M` if ($tpe == M) then set ${$pe} = M <--- This doesn't work else endif end In this case what... (2 Replies)
Discussion started by: wxornot
2 Replies

9. Linux

How do i set environment variable

Hi, I am quite new to Linux. And I have doubt how to set new environment variable with value to a C executable. Let say I have a environment variable $Hack ; I would like to load a value for this variable; so that when the C executable is executed, the $Hack would set the variable value. ... (4 Replies)
Discussion started by: ahjiefreak
4 Replies

10. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question