note 50008 modified in function.func-get-args by danbrown
| From: | danbrown@php.net | Date: | Sat, 19 Jul 2008 21:47:24 +0000 |
| Subject: | note 50008 modified in function.func-get-args by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-141765@lists.php.net to get a copy of this message | ||
<?php
# Another attempt at named args (perl-inspired):
# list_to_assoc('key', 'value', 'key', 'value', ...) =>
# pairs[]
function list_to_assoc() {
$list = func_get_args();
$assoc = array();
while ($list and count($list) > 1) {
$assoc[array_shift($list)] = array_shift($list);
}
if ($list) { $assoc[] = $list[0]; }
return $assoc;
}
# Usage:
function example($required) {
$args = func_get_args(); array_shift($args); # drop 'required'
$rest = list_to_assoc($args);
echo "$required\n" . $rest['comment'];
}
?>
example("This is required...",
'comment', 'this is not.'); # this is like 'comment' =>
'this is not'
--was--
# Another attempt at named args (perl-inspired):
# list_to_assoc('key', 'value', 'key', 'value', ...) =>
# pairs[]
function list_to_assoc() {
$list = func_get_args();
$assoc = array();
while ($list and count($list) > 1) {
$assoc[array_shift($list)] = array_shift($list);
}
if ($list) { $assoc[] = $list[0]; }
return $assoc;
}
# Usage:
function example($required) {
$args = func_get_args(); array_shift($args); # drop 'required'
$rest = list_to_assoc($args);
echo "$required\n" . $rest['comment'];
}
example("This is required...",
'comment', 'this is not.'); # this is like 'comment' =>
'this is not'
http://php.net/manual/en/function.func-get-args.php