Array Functions

PHP array_fill_keys() Function

Topic: PHP Array ReferencePrev|Next

Description

The array_fill_keys() function fills an array with a value and lets you specify what key to use.

The following table summarizes the technical details of this function.

Return Value: Returns the filled array.
Version: PHP 5.2.0+

Syntax

The basic syntax of the array_fill_keys() function is given with:

array_fill_keys(keys, value);

The following example shows the array_fill_keys() function in action.

<?php
// Defining keys array
$keys = array("foo", "bar", "baz");

// Filling array
$result = array_fill_keys($keys, "hello");
print_r($result);
?>

Parameters

The array_fill_keys() function accepts the following parameters.

Parameter Description
keys Required. Specifies the array of values that will be used as keys. Illegal values for the key will be converted to string.
value Required. Specifies the value to use for filling the array.

More Examples

Here're some more examples showing how array_diff_ukey() function actually works:

You can also use the range() function to quickly create key range, like this:

<?php
// Defining keys array
$keys = range(1, 6);

// Filling array
$result = array_fill_keys($keys, "apple");
print_r($result);
?>

You can also specify negative indices as shown in the following example:

<?php
// Defining keys array
$keys = range(-2, 3);

// Filling array
$result = array_fill_keys($keys, "banana");
print_r($result);
?>
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties