PHP: Using variable variables. 
April 28th, 2008, 07:37 AM
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,859
| |
Using the code below you can turn array indices into variables that can be accessed as a normal variable would be. -
<?php
-
/**
-
* @author Mark Skilbeck - MAHCUZ.COM
-
* @title PHP: Variable variables.
-
**/
-
-
$array = array("Name" => "Foo", "Age" => 21);
-
-
foreach( $array AS $Key => $Val )
-
{
-
$var = $Key;
-
$$var = $Val;
-
}
-
-
// the array can now be accessed
-
// by using:
-
//
-
// echo $array_key;
-
-
echo "Name: $Name - Age: $Age";
-
// prints Name: Foo - Age: 21
-
?>
-
| 
April 29th, 2008, 09:39 PM
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,859
| | | re: PHP: Using variable variables.
Just found out you can use the extract() function instead.
My work is pointless!
| 
May 5th, 2008, 04:22 PM
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: PHP: Using variable variables. Quote: |
Originally Posted by markusn00b Just found out you can use the extract() function instead.
My work is pointless! | No it is not. At leastt you showed how you can use and manipulate dynamic variable names.
Ronald
| 
May 20th, 2008, 02:13 PM
|  | Expert | | Join Date: Jan 2008 Location: Bath, UK
Posts: 1,609
| | | re: PHP: Using variable variables. Quote: |
Originally Posted by ronverdonk At least you showed how you can use and manipulate dynamic variable names.
Ronald | Yeah, I never knew about $$ trick.
Thanks Mark...
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|