Home  >  Article  >  Backend Development  >  How to add key number sequence to array in php

How to add key number sequence to array in php

PHPz
PHPzOriginal
2023-04-27 16:38:201216browse

When performing PHP array operations, sometimes you need to add a series of key values ​​​​to the array. You can usually use a for loop to complete this task. In PHP, some built-in functions are provided to help us automatically generate number sequences, thereby avoiding the trouble and errors of manually writing for loops.

The following will introduce three common methods for adding key number sequences to arrays in PHP.

Method 1: array_combine() function

The array_combine() function can combine two arrays into one array, with one array as key and the other as value. When you only need to add keys to the array, you can pass in a "key array" and a "value array" of the same length, and then use the array_combine() function to merge the two arrays.

Sample code:

$keys = range(1, 10); //建立一个从1到10的数组
$values = array_fill(0, 10, 'value'); //建立一个value为'value'的10个元素的数组
$newArray = array_combine($keys, $values);
print_r($newArray);

Output result:

Array
(
    [1] => value
    [2] => value
    [3] => value
    [4] => value
    [5] => value
    [6] => value
    [7] => value
    [8] => value
    [9] => value
    [10] => value
)

Method 2: array_fill_keys() function

array_fill_keys() function can add all the keys of an array Set to the same value, this function requires two parameters: parameter one is the array containing the key to be changed, and parameter two is the fill value of the new key.

Sample code:

$keys = range(1, 10); //建立一个从1到10的数组
$newKeys = array_fill_keys($keys, 'value');
print_r($newKeys);

Output result:

Array
(
    [1] => value
    [2] => value
    [3] => value
    [4] => value
    [5] => value
    [6] => value
    [7] => value
    [8] => value
    [9] => value
    [10] => value
)

Method 3: array_map() function

array_map() function can take an array as a parameter, and Applies a callback function to each of its elements. This function can be used to assign a unique key value to each element in the array. First, you need to define a callback function that operates on array elements and returns an array with new keys.

Sample code:

$keyArray = range(1, 10); //建立一个从1到10的数组
$newArray = array_map(function($value) { return ['key' => $value]; }, $keyArray);
print_r($newArray);

Output result:

Array
(
    [0] => Array
        (
            [key] => 1
        )

    [1] => Array
        (
            [key] => 2
        )

    [2] => Array
        (
            [key] => 3
        )

    [3] => Array
        (
            [key] => 4
        )

    [4] => Array
        (
            [key] => 5
        )

    [5] => Array
        (
            [key] => 6
        )

    [6] => Array
        (
            [key] => 7
        )

    [7] => Array
        (
            [key] => 8
        )

    [8] => Array
        (
            [key] => 9
        )

    [9] => Array
        (
            [key] => 10
        )

)

Conclusion:

The above three methods allow us to quickly add digital keys to the array. Make it easier to use, operate and output. Among them, the array_combine() function is suitable for adding new keys to an array of existing values, the array_fill_keys() function is suitable for replacing existing keys with new key values, and the array_map() function is suitable for processing array elements. to create an array with new key values.

The above is the detailed content of How to add key number sequence to array 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