RE: bananasims at hotmail dot com
I also needed a work around to not having a new version of PHP and wanting my own keys. bananasims code doesn't like having an array as the second parameter...
Here's a slightly modified version than can handle 2 arrays as inputs:
//we want these values to be keys
$arr1 = (0 => "abc", 1 => "def");
/we want these values to be values
$arr2 = (0 => 452, 1 => 128);
function array_fill_keys($keyArray, $valueArray) {
if(is_array($keyArray)) {
foreach($keyArray as $key => $value) {
$filledArray[$value] = $valueArray[$key];
}
}
return $filledArray;
}
array_fill_keys($arr1, $arr2);
returns:
abc => 452, def =>128
array_fill_keys
(PHP 5 >= 5.2.0)
array_fill_keys — Fill an array with values, specifying keys
Opis
Fills an array with the value of the value parameter, using the values of the keys array as keys.
Parametry
- keys
-
Array of values that will be used as keys
- value
-
Either an string or an array of values
Zwracane wartości
Returns the filled array
Przykłady
Example #1 array_fill_keys() example
<?php
$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
print_r($a);
?>
Powyższy przykład wyświetli:
Array ( [foo] => banana [5] => banana [10] => banana [bar] => banana )
array_fill_keys
Scratchy
02-May-2008 11:18
02-May-2008 11:18
bananasims at hotmail dot com
19-Dec-2006 02:03
19-Dec-2006 02:03
Some of the versions do not have this function.
I try to write it myself.
You may refer to my script below
function array_fill_keys($array, $values) {
if(is_array($array)) {
foreach($array as $key => $value) {
$arraydisplay[$array[$key]] = $values;
}
}
return $arraydisplay;
}
