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

search for in the

array_fill> <array_diff
Last updated: Fri, 11 Apr 2008

view this page in

array_fill_keys

(PHP 5 >= 5.2.0)

array_fill_keys — Fill an array with values, specifying keys

Opis

array array_fill_keys ( array $keys , mixed $value )

Fills an array with the value of the value parameter, using the values of the keys array as keys.

Parametry

keys

Array of values that will be used as keys

value

Either an string or an array of values

Zwracane wartości

Returns the filled array

Przykłady

Example #1 array_fill_keys() example

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

Powyższy przykład wyświetli:

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



add a note add a note User Contributed Notes
array_fill_keys
Scratchy
02-May-2008 11: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 02: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> <array_diff
Last updated: Fri, 11 Apr 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites