Re: extract() question

From: Date: Mon, 19 Nov 2001 11:16:35 +0000
Subject: Re: extract() question
Groups: php.general 
Request: Send a blank email to php-general+get-75100@lists.php.net to get a copy of this message
I needed this once, but I could not figure out how to just create the vars in current scope. If my_extract() is called from within a function, then the variables will not be available unless you declare them global (which kinda makes the function pointless). Any ideas? -Jason Garber IonZoft.com At 06:55 PM 11/18/2001 -0500, Joe Stump wrote:
You may want to do something like this instead: <? function my_extract($arr) {
    if(is_array($arr) && sizeof($arr))
    {
      while(list($key,$val) = each($arr)
      {
        $new_var = str_replace(' ','_',$key);
        global $$new_var;
        $$new_var = $val;
      }
    }
} ?> --Joe On Sun, Nov 18, 2001 at 04:21:46PM -0500, David Bernier wrote: There is this array which I would like to convert into a series variables using the extract function: <? $oz = array( "lion" => "courage", "dorothy" => "kansas", "scarecrow" => "brain" "tin man" => "heart"); extract($oz); ?> now, I would like to access my new variables. it is obviously easy for $lion, $dorothy, and $scarecrow but it isn't for "tin man". from this, I have 3 or 4 questions: 1) has $oz["tin man"] been passed into a variable? 2) if yes, how do I access the variable that came out of $oz["tin man"]? 3) let's pretend that I have no control over the names of the keys for $oz, how should I have called extract() to tell it to replace the space between "tin" and "man" by a underscore character? 4) finally, is there a way to access and retrieve that values of the symbol table without knowing their names? David Joe Stump <joe@joestump.net> ------------------------------------------------------------------------ "How would this sentence be different if pi equaled 3?"


« previous php.general (#75100) next »