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: Mon, 05 Feb 2007

view this page in

array_fill_keys

(PHP 5 >= 5.2.0)

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

説明

array array_fill_keys ( array keys, mixed value )

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

例 222. array_fill_keys() の例

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

$a は次のようになります。


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

    

array_fill() および array_combine() も参照ください。



add a note add a note User Contributed Notes
array_fill_keys
php dot spam at phihag dot de
05-Jan-2007 07:44
A better(presumably faster, easier to write) substitute (for php >= 5) is:

<?php
if (! function_exists("array_fill_keys")) {
   function
array_fill_keys(array $keys, $value) {
       return
array_combine($keys, array_fill(0, count($keys), $value));
   }
}

// simple testcase
(array_fill_keys(array(1, 3, 2, "a", "b"), 42) === array(1 => 42, 3 => 42, 2 => 42, "a" => 42, "b" => 42)) or die("testcase 1 of array_fill_keys failed.");
?>
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: Mon, 05 Feb 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites