Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: array_fill_keys - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

array_fill" width="11" height="7"/> <array_diff
Last updated: Fri, 20 Jun 2008

view this page in

array_fill_keys

(PHP 5 >= 5.2.0)

array_fill_keys — キーを指定して、配列を値で埋める

説明

array array_fill_keys ( array $keys , mixed $value )

パラメータ value で指定した値で配列を埋めます。 キーとして、配列 keys で指定した値を使用します。

パラメータ

keys

キーとして使用する値の配列。キーとして無効な値は string に変換されます。

value

埋めたい値。

返り値

値を埋めた配列を返します。

例1 array_fill_keys() の例

<?php
$keys 
= array('foo'510'bar');
$a array_fill_keys($keys'banana');
print_r($a);
?>

上の例の出力は以下となります。

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



array_fill" width="11" height="7"/> <array_diff
Last updated: Fri, 20 Jun 2008
 
add a note add a note User Contributed Notes
array_fill_keys
matrebatre
20-Jun-2008 11:28
This function does the same as:
<?php
$array
= array_combine($keys,array_fill(0,count($keys),$value));
?>
phydeaux
15-May-2008 02:26
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] =>
Scratchy
03-May-2008 06:18
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
bananasims at hotmail dot com
19-Dec-2006 10:03
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;
}

array_fill" width="11" height="7"/> <array_diff
Last updated: Fri, 20 Jun 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites