You are here: Bind variables to a simple array « PHP Programming Language « IT news, forums, messages
Bind variables to a simple array

Posted by Andreas Thiele on 10/31/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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация