PHP 8.4.0 RC4 available for testing

array_fill_keys

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

array_fill_keysBefüllt ein Array mit Werten mit den übergebenen Schlüsseln

Beschreibung

array_fill_keys(array $keys, mixed $value): array

Befüllt ein Array mit dem Wert, der im Parameter value übergeben wurde, und verwendet dabei die Werte des Arrays keys als Schlüssel.

Parameter-Liste

keys

Ein Array mit den Werten, die als Schlüssel verwendet werden. Für Schlüssel ungültige Werte werden in einen String konvertiert.

value

Der Wert, der zum Befüllen des Arrays verwendet wird

Rückgabewerte

Gibt das befüllte Array zurück

Beispiele

Beispiel #1 array_fill_keys()-Beispiel

<?php
$keys
= array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'Banane');
print_r($a);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Siehe auch

  • array_fill() - Füllt ein Array mit Werten
  • array_combine() - Erzeugt ein Array, indem es ein Array für die Schlüssel und ein anderes für die Werte verwendet

add a note

User Contributed Notes 6 notes

up
67
sergli at nigma dot ru
12 years ago
<?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.
up
31
atul dot kr_singh at hotmail dot com
11 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.
<?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
)
)
up
3
ray.paseur sometimes uses gmail
2 years ago
Get an associative array of zeros for counting letter frequency

<?php
$ltrs
= array_fill_keys( range('a', 'z'), 0 );
up
-2
Scratchy
16 years ago
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
up
-4
bananasims at hotmail dot com
17 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 => $value) {
$arraydisplay[$array[$key]] = $values;
}
}
return $arraydisplay;
}
up
-4
manavchugh988 at gmail dot com
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---->>>

<?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
)
To Top