note 83185 added to function.array-fill-keys
| From: | phydeaux at osu1 dot php dot net | Date: | Wed, 14 May 2008 17:26:25 +0000 |
| Subject: | note 83185 added to function.array-fill-keys | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139376@lists.php.net to get a copy of this message | ||
Scratchy's version still doesn't work like the definition describes. Here's one that
can take a mixed variable as the second parameter, defaulting to an empty string if it's not
specified. Don't know if this is exactly how the function works in later versions but
it's at least a lot closer.
function array_fill_keys($target, $value = '') {
if(is_array($target)) {
foreach($target as $key => $val) {
$filledArray[$val] = is_array($value) ? $value[$key] : $value;
}
}
return $filledArray;
}
This works for either strings or numerics, so if we have
$arr1 = array(0 => 'abc', 1 => 'def');
$arr2 = array(0 => 452, 1 => 128);
$arr3 = array(0 => 'foo', 1 => 'bar');
then
array_fill_keys($arr1,$arr2)
returns: [abc] => 452, [def] => 128
array_fill_keys($arr1,0)
returns: [abc] => 0, [def] => 0
array_fill_keys($arr2,$arr3)
returns: [452] => foo, [128] => bar
array_fill_keys($arr3,'BLAH')
returns: [foo] => BLAH, [bar] => BLAH
and array_fill_keys($arr1)
returns: [abc] =>, [def] =>
----
Server IP: 208.69.120.35
Probable Submitter: 64.191.171.77
----
Manual Page -- http://www.php.net/manual/en/function.array-fill-keys.php
Edit -- https://master.php.net/note/edit/83185
Del: integrated -- https://master.php.net/note/delete/83185/integrated
Del: useless -- https://master.php.net/note/delete/83185/useless
Del: bad code -- https://master.php.net/note/delete/83185/bad+code
Del: spam -- https://master.php.net/note/delete/83185/spam
Del: non-english -- https://master.php.net/note/delete/83185/non-english
Del: in docs -- https://master.php.net/note/delete/83185/in+docs
Del: other reasons-- https://master.php.net/note/delete/83185
Reject -- https://master.php.net/note/reject/83185
Search -- https://master.php.net/manage/user-notes.php