Page 1 of 1
Creating dynamic variables, change variable names
Posted: Wed Sep 30, 2009 4:58 pm
by black_hat
I'm pulling four strings from a database, I want to create PHP variables named one for each of these strings.
for each string s do
create variable ($s)
resulting in
<?php>
....
$X
$X
$Y
$Z
...
</?>
Is this even possible or would I need to go through a file manipulator script?
Re: Creating dynamic variables, change variable names
Posted: Wed Sep 30, 2009 5:09 pm
by Mark Baker
Code: Select all
foreach($string as $s) {
$$s = null;
}
Re: Creating dynamic variables, change variable names
Posted: Wed Sep 30, 2009 5:09 pm
by requinix
There are very, very few reasons why doing that would be a good idea.
What is this code supposed to do?
Re: Creating dynamic variables, change variable names
Posted: Wed Sep 30, 2009 5:14 pm
by black_hat
I'm pulling a big query from the DB, ordering by valueName, for this example let's say the valueName are human names (john, Jack, etc.).
So the query comes back as
John __*__1
John __*__2
John __*__44
Jack __*__3
Jack __*__21
Jill __*__533
Jill __*__213
Jill __*__11
Jil __*__12
I want to split this large return into lists identified by their valueName. Such as,
$John = array(1,2,44);
$Jack = array(3,21);
$Jill = array ("533,213,11,12);
To which I will feed into a graphing function to draw each as its own line, and creating a graph legend based on the array name given. I'd rather split it like this than have to modify and add logic to the graphing function.
Re: Creating dynamic variables, change variable names
Posted: Wed Sep 30, 2009 5:33 pm
by peterjwest
As tasairis suggested, this is a bad way of programming. Variable variables are pretty dumb and unneccessary.
Quite simply you can make an array like this:
Code: Select all
$people = array(
'John' => array(1,2,44),
'Jack' = array(3,21),
'Jill' = array (533,213,11,12)
)
You can pass this array into the grapher, or pass each value/key.
e.g.
Code: Select all
graph($people);
//or..
foreach($people as $name => $person) {
graph($people[$name],$name);
}
Re: Creating dynamic variables, change variable names
Posted: Wed Sep 30, 2009 5:38 pm
by black_hat
Perfect, exactly what I need. Mind fudge, I completely forgot about that