This is a discussion on Variable question within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hey, I'm trying to define a variable that has a variable in it, can this be done and what ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hey,
I'm trying to define a variable that has a variable in it, can this be done and what would be the syntax. I read the PHP manual section on variable variables but this is not what I'm looking for. This is what I'm trying to do: I'm loading values into an array and I need to define various different name arrays that have a common part in the name. $myarray1$variable[] = ( x value ); $myarray2$variable[] = ( x value ); Thanks, Carlos |
|
|||
|
On 2004-03-02, Carlos Ramirez wrote:
> Hey, > > I'm trying to define a variable that has a variable in it, can this be > done and what would be the syntax. > > I read the PHP manual section on variable variables but this is not what > I'm looking for. > > This is what I'm trying to do: > > I'm loading values into an array and I need to define various different > name arrays that have a common part in the name. > > $myarray1$variable[] = ( x value ); > $myarray2$variable[] = ( x value ); > > Thanks, > > Carlos > Does this help? <?php $myarray1 = "array_one"; $myarray2 = "array_two"; $variable = "_var"; ${$myarray1.$variable} = array(1, 2, 3); ${$myarray2.$variable} = array('a', 'b', 'c'); $this_var1 = $myarray1.$variable; $this_var2 = $myarray2.$variable; print_r($$this_var1); print_r($$this_var2); print_r($array_one_var); print_r($array_two_var); ?> -- Mike Peters mike [-AT-] ice2o [-DOT-] com http://www.ice2o.com |
|
|||
|
Carlos Ramirez wrote:
> Hey, > > I'm trying to define a variable that has a variable in it, can this be > done and what would be the syntax. > > I read the PHP manual section on variable variables but this is not what > I'm looking for. > > This is what I'm trying to do: > > I'm loading values into an array and I need to define various different > name arrays that have a common part in the name. > > $myarray1$variable[] = ( x value ); > $myarray2$variable[] = ( x value ); > > Thanks, > > Carlos $prefix = '$myarray'; $suffix = $variable; $code = $prefix . $suffix . "[] = ($value);"; // don't forget that ';' eval($code); Check here, this is good: http://www.php.net/manual/en/function.eval.php -- Nelson Menezes http://burninghorizons.net |