Home >Backend Development >PHP Tutorial >How to fill an array with specified key names using array_fill_keys function in PHP

How to fill an array with specified key names using array_fill_keys function in PHP

WBOY
WBOYOriginal
2023-06-26 11:30:55790browse

In the PHP programming process, arrays are widely used, especially in data processing and data storage. Among them, the array_fill_keys function can be used to quickly create an array with specified key names and fill in the default values, greatly simplifying the code. This article will introduce how to use the array_fill_keys function in PHP to fill an array with specified key names.

1. Basic usage

The basic syntax of the array_fill_keys function is as follows:

array array_fill_keys ( array $keys , mixed $value )

Among them, $keys is the array of key names to be filled into the array, and $value is the array to be filled. the default value.

For example:

$keys = array('apple', 'banana', 'orange');
$values = 0;
$fruit_shop = array_fill_keys($keys, $values);
print_r($fruit_shop);

The running results are as follows:

Array
(
    [apple] => 0
    [banana] => 0
    [orange] => 0
)

In this example, we first define a key array $keys, which contains apples and bananas , orange are the names of three fruits. Then, we define the default value $values ​​as 0 and use the array_fill_keys function to generate the fruit inventory list $fruit_shop of the fruit shop.

2. Multi-dimensional array filling

In addition to single-dimensional arrays, we can also use the array_fill_keys function to generate multi-dimensional arrays. This is useful when large batches of data need to be processed, and equal amounts of locked values ​​can be generated quickly.

A simple example:

$keys = array('apple', 'banana', 'orange');
$values = 0;
$fruit_shop = array_fill_keys($keys, array_fill_keys($keys, $values));
print_r($fruit_shop);

The running results are as follows:

Array
(
    [apple] => Array
        (
            [apple] => 0
            [banana] => 0
            [orange] => 0
        )

    [banana] => Array
        (
            [apple] => 0
            [banana] => 0
            [orange] => 0
        )

    [orange] => Array
        (
            [apple] => 0
            [banana] => 0
            [orange] => 0
        )

)

In this example, we generated a fruit store’s fruit inventory list $fruit_shop, this list , the default inventory of each fruit is set to 0. At the same time, this list also consists of three single-dimensional arrays with the same key names as apple, banana, and orange. In this way, we can add data such as quantity or sales quantity for each fruit in this list.

3. Advanced Usage

The array_fill_keys function can not only fill in default values, but also fill in object instances. This is very useful for developers using object-oriented programming.

For example:

class Fruit {
    public $name;
    public $price;
    public function __construct($name, $price) {
        $this->name = $name;
        $this->price = $price;
    }
}
$keys = array('apple', 'banana', 'orange');
$values = new Fruit('N/A', 0);
$fruit_shop = array_fill_keys($keys, $values);
print_r($fruit_shop);

The running results are as follows:

Array
(
    [apple] => Fruit Object
        (
            [name] => N/A
            [price] => 0
        )

    [banana] => Fruit Object
        (
            [name] => N/A
            [price] => 0
        )

    [orange] => Fruit Object
        (
            [name] => N/A
            [price] => 0
        )

)

In this example, we created a fruit class Fruit with two attributes: $name and $price. Then, we used the array_fill_keys function to generate a fruit store's fruit inventory list $fruit_shop. The default inventory of each fruit in this list is a Fruit object instance. Using this inventory list, we can easily perform batch modifications or data calculations on the attributes of each fruit.

In general, the array_fill_keys function is often used in PHP programming. It can provide great help in business scenarios such as processing large amounts of data and processing data in large batches. The above three methods are only one of the application scenarios of the array_fill_keys function. We hope readers can apply it more in daily programming practice to improve programming efficiency and development experience.

The above is the detailed content of How to fill an array with specified key names using array_fill_keys function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn