©
This document uses PHP Chinese website manual Release
(PHP 5 >= 5.2.0, PHP 7)
array_fill_keys — 使用指定的键和值填充数组
$keys
, mixed $value
) 使用 value
参数的值作为值,使用 keys
数组的值作为键来填充一个数组。
keys
使用该数组的值作为键。非法值将被转换为 字符串 。
value
填充使用的值。
返回填充后的数组。
Example #1 array_fill_keys() 范例
<?php
$keys = array( 'foo' , 5 , 10 , 'bar' );
$a = array_fill_keys ( $keys , 'banana' );
print_r ( $a );
?>
以上例程会输出:
Array ( [foo] => banana [5] => banana [10] => banana [bar] => banana )
[#1] ntd at entidi dot it [2015-10-23 19:04:15]
To remove arbitrary keys from an associative array:
<?php
function nuke_keys($keys, $array) {
return array_diff_key($array, array_fill_keys($keys, 0));
}
$array = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$keys = array('red', 'purple');
print_r(nuke_keys($keys, $array));
?>
The above snippet will return:
Array
(
[blue] => 1
[green] => 3
)
[#2] taylanaktepe at yahoo dot com [2014-07-21 09:46:15]
$keys = array(1, 2, 3);
// Fill it with value.
$keys = array_fill_keys($keys, 'banana');
print_r($keys);
// Fill it different value.
$apples = array_fill_keys(array_keys($keys), 'apple');
print_r($apples);
// Output:
Array (
[1] => banana
[2] => banana
[3] => banana
)
Array (
[1] => apple
[2] => apple
[3] => apple
)
[#3] atul dot kr_singh at hotmail dot com [2013-01-04 11:39:10]
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
)
)
[#4] sergli at nigma dot ru [2012-05-25 11:53:22]
<?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.
[#5] matrebatre [2008-06-20 07:28:13]
This function does the same as:
<?php
$array = array_combine($keys,array_fill(0,count($keys),$value));
?>
[#6] phydeaux [2008-05-14 10:26:25]
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] =>
[#7] Scratchy [2008-05-02 14:18:03]
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
[#8] bananasims at hotmail dot com [2006-12-19 05:03:24]
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;
}