Re: [PHP] foreach loop to set variables




On Fri, 2008-04-25 at 06:12 -0700, 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) {
echo "$key = $value";
}

This echo's all the data how I would expect. But taking the echo out to
have:

foreach ($formdata as $key => $value) {
$key = $value;
}

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;
}

How about $this->formdata?

.



Relevant Pages

  • Re: [PHP] foreach loop to set variables
    ... On 25 Apr 2008, at 14:12, jamest wrote: ... create some variables from the array's keys and values. ... the foreach but with no success. ...
    (php.general)
  • Re: Getting variable names passed to form in php
    ... Suppose I just want to print all the keys... ... echo 'Hello World!\n'; ... it prints an error message of the foreach line ...
    (comp.lang.php)
  • Threads and Directory Handles
    ... sub file_stats{ ... foreach my $file (keys %$files){ ... This is perl, v5.10.0 built for MSWin32-x86-multi-thread ...
    (comp.lang.perl.misc)
  • Re: sequence composition
    ... my %s;# a hash of arrays, to hold each line of sequence ... #a hash to hold the AA sequences. ... foreach my $k (keys %seq){ ...
    (perl.beginners)
  • Re: Font Smoothing & Image Scaling
    ... > Hi Echo, ... > and almost totally pointless but fun nevertheless". ... Usually our bad pun threads revolve around food -- ... > I have arthritic fingers and have to press the keys with my nose. ...
    (microsoft.public.powerpoint)