PHP: Hypertext Preprocessor (original) (raw)

array_fill_keys

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

array_fill_keys — Fill an array with values, specifying keys

Description

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'); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mo>=</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><msub><mi>y</mi><mi>f</mi></msub><mi>i</mi><mi>l</mi><msub><mi>l</mi><mi>k</mi></msub><mi>e</mi><mi>y</mi><mi>s</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">a = array_fill_keys(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.10764em;">f</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0197em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.03148em;">k</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">eys</span><span class="mopen">(</span></span></span></span>keys, 'banana'); print_r($a); ?>

The above example will output:

Array ( [foo] => banana [5] => banana [10] => banana [bar] => banana )

See Also

Found A Problem?

sergli at nigma dot ru

12 years ago

`a=array("1");vardump(arrayfillkeys(a = array("1");var_dump(array_fill_keys(a=array("1");vardump(arrayfillkeys(a, "test")); ?>

array(1) {
[1]=>
string(4) "test"
}

now string key "1" become an integer value 1, be careful.

`

atul dot kr_singh at hotmail dot com

12 years ago

`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.
"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
)
)`

ray.paseur sometimes uses gmail

3 years ago

`Get an associative array of zeros for counting letter frequency

"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=>key => key=>value) { filledArray[filledArray[filledArray[value] = valueArray[valueArray[valueArray[key]; } } return $filledArray; } array_fill_keys($arr1, $arr2); returns: abc => 452, def =>128 ` [ **_bananasims at hotmail dot com_**](#71845)[ ¶](#71845) **18 years ago** `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=>key => key=>value) { arraydisplay[arraydisplay[arraydisplay[array[$key]] = $values; } } return $arraydisplay; } ` [ **_manavchugh988 at gmail dot com_**](#127185)[ ¶](#127185) **2 years ago** `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---->>> a=array("a","b","c","d","e");//newarraywithasinglesamevaluea = array("a","b","c","d","e");//new array with a single same value a=array("a","b","c","d","e");//newarraywithasinglesamevaluenewArray = array_fill_keys($a, "Testing");//printing the array echo "
";  
print_r($newArray);  
 echo "
"; ?>

output;
Array
(
[a] => Testing
[b] => Testing
[c] => Testing
[d] => Testing
[e] => Testing
)

`