|  | Posted by Andreas Thiele on 06/18/99 11:23 
Hi,
 I'm very new to php. In my current code I'd like to bind variables to the
 values of a simply (only indexed by numbers [s.
 http://www.php-center.de/en-html-manual/language.types.array.html]) array.
 Of course I noticed the function extract(), but this works on string
 indices. So I had the following idea:
 
 <?PHP
 
 function abind($array, $names) {
 // Return source binding the given $names to the elements
 // of the simple/linear array $array.
 $r = "";
 for ($i = 0; $i < count($names); $i++) {
 $r = $r."\$".current($names)." = \$".$array."[".$i."];";
 next($names);
 }
 return $r;
 }
 
 function test($arr) {
 // create bindings for $foo, $bar and $baz by evaluating "$foo =
 $arr[0]; ..."
 eval(abind("arr", array("foo", "bar", "baz")));
 return $foo."-".$bar."-".$baz;
 }
 
 echo test(array(1, 2, 3));
 
 ?>
 
 Of course function test will use $foo, $bar and $baz really oftern. I could
 as well use $arr[0] and so on, but I'd like to use more describing variable
 names. In this case the eval line would make things more succint and thus
 improve code readability.
 
 Am I missing something? Is this a standard/idiomatic way in php or is there
 some other?
 
 
 Thanks for your hints.
 
 Andreas
 [Back to original message] |