Home » PHP

PHP array_fill_keys() Function with Example

PHP array_fill_keys() Function: Here, we are going to learn about the array_fill_keys() function with example in PHP.
Submitted by IncludeHelp, on February 10, 2019

PHP array_fill_keys() Function

array_fill_keys() function is used to fill a given array with a value where keys are specified. We will use an array that contains keys, all these keys will be filled with the value and return a new array.

Syntax:

    array_fill_keys(keys, value) : array

Here,

  • keys is an array and its value will be used as keys for the new array.
  • value to be filled for the keys.

Examples:

    Input:
    $keys = array("Name", "Age", "Gender", "Address"); 
    value = "N/A"

    Function call:
    array_fill_keys($keys, "N/A");
    Output:
    Array
    (
        [Name] => N/A
        [Age] => N/A
        [Gender] => N/A
        [Address] => N/A
    )

PHP code:

<?php
    //keys (creating array)
    $keys = array("Name", "Age", "Gender", "Address");
    
    //creating an array with these keys
    //and, filling all keys with default value
    $arr = array_fill_keys($keys, "N/A");
    
    print_r ($arr);
    
?>

Output

Array
(
    [Name] => N/A
    [Age] => N/A
    [Gender] => N/A
    [Address] => N/A
)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.