array_fill_keys
(PHP 5 >= 5.2.0)
array_fill_keys — Fill an array with values, specifying keys
Description
Fills an array with the
value of the value
parameter, using the
values of the keys
array as keys.
Parameters
-
keys
-
Array of values that will be used as keys. Illegal values for key will be converted to string.
-
value
-
Value to use for filling
Return Values
Returns the filled array
Examples
Example #1 array_fill_keys() example
<?php
$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
print_r($a);
?>
The above example will output:
Array ( [foo] => banana [5] => banana [10] => banana [bar] => banana )
See Also
- array_fill() - Fill an array with values
- array_combine() - Creates an array by using one array for keys and another for its values
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения, относящиеся к переменным и типам
- Массивы
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_exists
- array_keys
- array_map
- array_merge_recursive
- array_merge
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace_recursive
- array_replace
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff_assoc
- array_udiff_uassoc
- array_udiff
- array_uintersect_assoc
- array_uintersect_uassoc
- array_uintersect
- array_unique
- array_unshift
- array_values
- array_walk_recursive
- array_walk
- array
- arsort
- asort
- compact
- count
- current
- each
- end
- extract
- in_array
- key_exists
- key
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort
Коментарии
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;
}
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
<?php
$a = array("1");
var_dump(array_fill_keys($a, "test"));
?>
array(1) {
[1]=>
string(4) "test"
}
now string key "1" become an integer value 1, be careful.
If an associative array is used as the second parameter of array_fill_keys, then the associative array will be appended in all the values of the first array.
e.g.
<?php
$array1 = array(
"a" => "first",
"b" => "second",
"c" => "something",
"red"
);
$array2 = array(
"a" => "first",
"b" => "something",
"letsc"
);
print_r(array_fill_keys($array1, $array2));
?>
The output will be
Array(
[first] => Array(
[a] => first,
[b] => something,
[0] => letsc
),
[second] => Array(
[a] => first,
[b] => something,
[0] => letsc
),
[something] => Array(
[a] => first,
[b] => something,
[0] => letsc
),
[red] => Array(
[a] => first,
[b] => something,
[0] => letsc
)
)
Get an associative array of zeros for counting letter frequency
<?php
$ltrs = array_fill_keys( range('a', 'z'), 0 );
see array_fill_keys are basically used to make a new array from a pre-existing array in a form that the value of the pre-existing array will now be the key of the new Array .And there value will be same That we had given in the 2nd parameter . Example Below---->>>
<?php
//pre existing array
$a = array("a","b","c","d","e");
//new array with a single same value
$newArray = array_fill_keys($a, "Testing");
//printing the array
echo "<pre>";
print_r($newArray);
echo "</pre>";
?>
output;
Array
(
[a] => Testing
[b] => Testing
[c] => Testing
[d] => Testing
[e] => Testing
)