Re: foreach loop to set variables

From: Date: Fri, 25 Apr 2008 13:42:04 +0000
Subject: Re: foreach loop to set variables
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-273558@lists.php.net to get a copy of this message
On 25 Apr 2008, at 14:12, jamest wrote:
I am passing an array to a class which I want to take the array data and create some variables from the array's keys and values. So I want to create (in this case 21) new variables that I want to create in the foreach but with no success. foreach ($formdata as $key => $value) { $key = $value; }
$$key = $value; They're called variable variables - search the manual for that term for more info.
But this doesn't work. The variables aren't set. I was thinking that I could set up the variables outside of the function by using public $variablename for all the different variables then set the variable value using: foreach ($formdata as $key => $value) { $this->key = $value; }
$this->$key = $value; -Stut -- http://stut.net/

« previous php.general (#273558) next »