- Php заполнить все пустые значения массива
- PHP Calendar Functions
- PHP Date/Time Functions
- PHP Directory Functions
- PHP Error
- PHP Filesystem Functions
- PHP FTP Functions
- PHP JASON
- PHP Math Functions
- PHP GMP Functions
- PHP MySQLi Functions
- PHP String Functions
- PHP Network Functions
- PHP IntlChar Functions
- PHP Image Processing and GD Functions
- PHP Gmagick Functions
- PHP Imagick Functions
- PHP ImagickDraw Functions
- PHP DsDeque Functions
- PHP DsMap Functions
- PHP DSSequence Functions
- PHP DSVector Functions
- PHP DsStack Functions
- PHP DsQueue Functions
- PHP DsPriorityQueue Functions
- PHP SPL Data structures
- PHP Basics
- PHP Advance
- PHP Calendar Functions
- PHP Date/Time Functions
- PHP Directory Functions
- PHP Error
- PHP Filesystem Functions
- PHP FTP Functions
- PHP JASON
- PHP Math Functions
- PHP GMP Functions
- PHP MySQLi Functions
- PHP String Functions
- PHP Network Functions
- PHP IntlChar Functions
- PHP Image Processing and GD Functions
- PHP Gmagick Functions
- PHP Imagick Functions
- PHP ImagickDraw Functions
- PHP DsDeque Functions
- PHP DsMap Functions
- PHP DSSequence Functions
- PHP DSVector Functions
- PHP DsStack Functions
- PHP DsQueue Functions
- PHP DsPriorityQueue Functions
- PHP SPL Data structures
- array_fill_keys
- Parameters
- Return Values
- Examples
- See Also
- User Contributed Notes 10 notes
Php заполнить все пустые значения массива
- PHP | Arrays
- PHP array() Function
- PHP | array_change_key_case() Function
- PHP array_chunk() Function
- PHP | array_column() Function
- PHP array_combine() Function
- PHP array_count_values() Function
- PHP array_diff() function
- PHP array_diff_assoc() Function
- PHP array_diff_key() Function
- PHP array_diff_uassoc() Function
- PHP array_diff_ukey() Function
- PHP array_fill() function
- PHP array_fill_keys() Function
- PHP Array Functions Complete Reference
PHP Calendar Functions
PHP Date/Time Functions
- PHP Date and Time
- PHP | checkdate() Function
- PHP | date_create(), date_format(), date_add() Functions
- PHP | date_create_from_format() Function
- PHP | date_date_set() Function
- PHP | date_default_timezone_get() Function
- PHP | date_default_timezone_set() Function
- PHP | date_diff() Function
- PHP | date_get_last_errors() Function
- PHP | date_isodate_set() Function
- PHP | date_modify() Function
- PHP | date_offset_get() Function
- PHP | date_parse_from_format() Function
- PHP | date_parse() Function
- PHP | date_sub() Function
PHP Directory Functions
PHP Error
PHP Filesystem Functions
- PHP basename( ) Function
- PHP chgrp( ) Function
- PHP chmod( ) Function
- PHP chown( ) Function
- PHP copy( ) Function
- PHP dirname( ) Function
- PHP disk_free_space( ) Function
- PHP disk_total_space( ) Function
- PHP feof( ) Function
- PHP fflush( ) Function
- PHP | fgetc( ) Function
- PHP | fgets( ) Function
- PHP | fgetss( ) Function
- PHP | file_exists( ) Function
- PHP file_get_contents() Function
- PHP Filesystem Functions Complete Reference
PHP FTP Functions
- PHP | ftp_alloc() function
- PHP | ftp_chdir() function
- PHP | ftp_close() function
- PHP | ftp_connect() function
- PHP | ftp_delete() function
- PHP | ftp_exec() function
- PHP | ftp_get() function
- PHP | ftp_put() function
- PHP | ftp_get_option() function
- PHP | ftp_login() function
- PHP | ftp_mdtm() Function
- PHP | ftp_mkdir() function
PHP JASON
PHP Math Functions
PHP GMP Functions
PHP MySQLi Functions
PHP String Functions
PHP Network Functions
PHP IntlChar Functions
PHP Image Processing and GD Functions
PHP Gmagick Functions
PHP Imagick Functions
PHP ImagickDraw Functions
PHP DsDeque Functions
PHP DsMap Functions
- PHP | Ds\Map allocate() Function
- PHP | Ds\Map apply() Function
- PHP | Ds\Map capacity() Function
- PHP | DS\Map clear() Function
- PHP | Ds\Map __construct() Function
- PHP Ds\Map copy() Function
- PHP | Ds\Map count() Function
- PHP | Ds\Map diff() Function
- PHP | Ds\Map filter() Function
- PHP | Ds\Map first() Function
- PHP Ds\Map Functions Complete Reference
PHP DSSequence Functions
PHP DSVector Functions
PHP DsStack Functions
PHP DsQueue Functions
PHP DsPriorityQueue Functions
PHP SPL Data structures
- SplDoublyLinkedList in PHP
- PHP SplDoublyLinkedList add() Function
- PHP SplDoublyLinkedList bottom() Function
- PHP SplDoublyLinkedList count() function
- PHP SplFixedArray count() Function
- PHP SplFixedArray current() Function
- PHP SplFixedArray getSize() Function
- PHP SplFixedArray key() Function
- PHP SplObjectStorage addAll() Function
- PHP SplObjectStorage attach() Function
- PHP SplObjectStorage contains() Function
- PHP SplObjectStorage count() Function
- PHP SplQueue::__construct() Function
- PHP SPL Data structures Complete Reference
PHP Basics
- PHP Tutorial
- PHP | Introduction
- PHP Full Form
- How to set PHP development environment in windows ?
- LAMP installation and important PHP configurations on Ubuntu
- PHP | Coding Standards
- PHP | Basic Syntax
- PHP | Variables
- PHP echo and print
- PHP | Data Types
- PHP | Strings
- PHP | Constants
- PHP | Magic Constants
- PHP | Decision Making
- PHP | Loops
- PHP | Superglobals
- PHP | Regular Expressions
- PHP Examples
PHP Advance
- Describe PHP Include and Require
- PHP | Basics of File Handling
- PHP | fopen( ) (Function open file or URL)
- PHP fread( ) Function
- PHP fclose( ) Function
- PHP | fwrite( ) Function
- PHP | Uploading File
- PHP Cookies
- PHP | Sessions
- PHP Filter and Filter Constant
- Implementing callback in PHP
- Exception Handling in PHP
- PHP | Arrays
- PHP array() Function
- PHP | array_change_key_case() Function
- PHP array_chunk() Function
- PHP | array_column() Function
- PHP array_combine() Function
- PHP array_count_values() Function
- PHP array_diff() function
- PHP array_diff_assoc() Function
- PHP array_diff_key() Function
- PHP array_diff_uassoc() Function
- PHP array_diff_ukey() Function
- PHP array_fill() function
- PHP array_fill_keys() Function
- PHP Array Functions Complete Reference
PHP Calendar Functions
PHP Date/Time Functions
- PHP Date and Time
- PHP | checkdate() Function
- PHP | date_create(), date_format(), date_add() Functions
- PHP | date_create_from_format() Function
- PHP | date_date_set() Function
- PHP | date_default_timezone_get() Function
- PHP | date_default_timezone_set() Function
- PHP | date_diff() Function
- PHP | date_get_last_errors() Function
- PHP | date_isodate_set() Function
- PHP | date_modify() Function
- PHP | date_offset_get() Function
- PHP | date_parse_from_format() Function
- PHP | date_parse() Function
- PHP | date_sub() Function
PHP Directory Functions
PHP Error
PHP Filesystem Functions
- PHP basename( ) Function
- PHP chgrp( ) Function
- PHP chmod( ) Function
- PHP chown( ) Function
- PHP copy( ) Function
- PHP dirname( ) Function
- PHP disk_free_space( ) Function
- PHP disk_total_space( ) Function
- PHP feof( ) Function
- PHP fflush( ) Function
- PHP | fgetc( ) Function
- PHP | fgets( ) Function
- PHP | fgetss( ) Function
- PHP | file_exists( ) Function
- PHP file_get_contents() Function
- PHP Filesystem Functions Complete Reference
PHP FTP Functions
- PHP | ftp_alloc() function
- PHP | ftp_chdir() function
- PHP | ftp_close() function
- PHP | ftp_connect() function
- PHP | ftp_delete() function
- PHP | ftp_exec() function
- PHP | ftp_get() function
- PHP | ftp_put() function
- PHP | ftp_get_option() function
- PHP | ftp_login() function
- PHP | ftp_mdtm() Function
- PHP | ftp_mkdir() function
PHP JASON
PHP Math Functions
PHP GMP Functions
PHP MySQLi Functions
PHP String Functions
PHP Network Functions
PHP IntlChar Functions
PHP Image Processing and GD Functions
PHP Gmagick Functions
PHP Imagick Functions
PHP ImagickDraw Functions
PHP DsDeque Functions
PHP DsMap Functions
- PHP | Ds\Map allocate() Function
- PHP | Ds\Map apply() Function
- PHP | Ds\Map capacity() Function
- PHP | DS\Map clear() Function
- PHP | Ds\Map __construct() Function
- PHP Ds\Map copy() Function
- PHP | Ds\Map count() Function
- PHP | Ds\Map diff() Function
- PHP | Ds\Map filter() Function
- PHP | Ds\Map first() Function
- PHP Ds\Map Functions Complete Reference
PHP DSSequence Functions
PHP DSVector Functions
PHP DsStack Functions
PHP DsQueue Functions
PHP DsPriorityQueue Functions
PHP SPL Data structures
- SplDoublyLinkedList in PHP
- PHP SplDoublyLinkedList add() Function
- PHP SplDoublyLinkedList bottom() Function
- PHP SplDoublyLinkedList count() function
- PHP SplFixedArray count() Function
- PHP SplFixedArray current() Function
- PHP SplFixedArray getSize() Function
- PHP SplFixedArray key() Function
- PHP SplObjectStorage addAll() Function
- PHP SplObjectStorage attach() Function
- PHP SplObjectStorage contains() Function
- PHP SplObjectStorage count() Function
- PHP SplQueue::__construct() Function
- PHP SPL Data structures Complete Reference
array_fill_keys
Fills an array with the value of the value parameter, using the values of the keys array as keys.
Parameters
Array of values that will be used as keys. Illegal values for key will be converted to string .
Return Values
Examples
Example #1 array_fill_keys() example
$keys = array( ‘foo’ , 5 , 10 , ‘bar’ );
$a = array_fill_keys ( $keys , ‘banana’ );
print_r ( $a );
?>?php
The above example will output:
Array ( [foo] => banana [5] => banana [10] => banana [bar] => banana )
See Also
- array_fill() — Fill an array with values
- array_combine() — Creates an array by using one array for keys and another for its values
User Contributed Notes 10 notes
var_dump ( array_fill_keys ( $a , «test» ));
?>
array(1) <
[1]=>
string(4) «test»
>
now string key «1» become an integer value 1, be careful.
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.
$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
)
)
Get an associative array of zeros for counting letter frequency
$ltrs = array_fill_keys ( range ( ‘a’ , ‘z’ ), 0 );
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;
>
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—->>>
//pre existing array
$a = array( «a» , «b» , «c» , «d» , «e» );
//new array with a single same value
$newArray = array_fill_keys ( $a , «Testing» );
echo «
" ;
print_r ( $newArray );
echo "
» ;
?>
output;
Array
(
[a] => Testing
[b] => Testing
[c] => Testing
[d] => Testing
[e] => Testing
)
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;
>
This function does the same as:
$array = array_combine ( $keys , array_fill ( 0 , count ( $keys ), $value ));
?>
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’);
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] =>