note 25314 added to language.variables.variable

From: Date: Thu, 19 Sep 2002 12:17:03 +0000
Subject: note 25314 added to language.variables.variable
Groups: php.notes 
Request: Send a blank email to php-notes+get-36950@lists.php.net to get a copy of this message
You may think of using variable variables to dynamically generate variables from an array, by doing something similar to: - <?php foreach ($array as $key => $value) { $$key= $value; } ?> This however would be reinventing the wheel when you can simply use: <?php extract( $array, EXTR_OVERWRITE); ?> Note that this will overwrite the contents of variables that already exist. Extract has useful functionality to prevent this, or you may group the variables by using prefixes too, so you could use: - EXTR_PREFIX_ALL <?php $array =array("one" => "First Value", "two" => "2nd Value", "three" => "8" ); extract( $array, EXTR_PREFIX_ALL, "my_prefix_"); ?> This would create variables: - $my_prefix_one $my_prefix_two $my_prefix_three containing: - "First Value", "2nd Value" and "8" respectively -- http://www.php.net/manual/en/language.variables.variable.php http://master.php.net/manage/user-notes.php?action=edit+25314 http://master.php.net/manage/user-notes.php?action=delete+25314 http://master.php.net/manage/user-notes.php?action=reject+25314

« previous php.notes (#36950) next »