Re: How to convert an array to variables?

From: Date: Sat, 15 Mar 2003 22:20:49 +0000
Subject: Re: How to convert an array to variables?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-139811@lists.php.net to get a copy of this message
Tom, This seems like what I need exactly, but I am having a bit of trouble getting it to work. Here is how I have worked around this, but I know there is a 'real' way to do this. Any thoughts? I had to set the variables, or should I just use the variables as ${dra_0} etc.? // get the values for the default dra from array while(list($key,$val) = each($r_p_dras)){
        $n = "dra_$key";
        echo $n;
        $$n = $val['dra_id'];
} $dra_a = "${dra_0}"; $dra_b = "${dra_1}"; $dra_c = "${dra_2}"; $dra_d = "${dra_3}"; $dra_e = "${dra_4}"; On Saturday, March 15, 2003, at 02:08 PM, Tom Rogers wrote:
Hi, Sunday, March 16, 2003, 4:52:10 AM, you wrote: CK> Hi all, CK> I have an array that gives me this when I do: CK> print_r($my_array); CK> Array CK> (
CK>      [0] => Array
CK>          (
CK>              [dra_id] => 5
CK>          )
CK>      [1] => Array
CK>          (
CK>              [dra_id] => 8
CK>          )
CK>      [2] => Array
CK>          (
CK>              [dra_id] => 9
CK>          )
CK> ) CK> using a foreach() I want to create a variable with a unique incremental CK> name for each of the values. CK> for example: CK> $dra_1 = 5; CK> $dra_2 = 8; CK> $dra_3 = 9; CK> How would I do this? A variable that has a variable name based on a CK> counter in the loop? Not sure the syntax for that. CK> Thanks CK> Charles something like this... while(list($key,$val) = each($my_array)){
        $n = 'dra_'.$key+1;
        $$n = $val['dra_id'];
} -- regards, Tom


« previous php.general (#139811) next »