Home >Backend Development >PHP Problem >How to change the keys of an array in php

How to change the keys of an array in php

PHPz
PHPzOriginal
2023-04-23 09:13:48529browse

In PHP, arrays are one of the most useful and commonly used data types. Arrays play an important role when dealing with multidimensional data and when storing and accessing data in memory. In some cases, we need to change the keys of an array for easier access to a specific value, which is a common need in array operations.

This article will explain how to use some built-in functions in PHP to change the keys of an array. We'll explore three common use cases:

  1. Changing the key names of an array

In PHP, we can use the array_keys function to generate an The key list of the array, and then use the array_combine function to pair the new key list with the value of the original array, so that the key name of the array can be changed.

The following is an example:

$oldArray = array(
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'cherry'
);

$newKeys = array(
    '1' =>'apple',
    '2' => 'banana',
    '3' => 'cherry'
);

$newArray = array_combine($newKeys, $oldArray);

print_r($newArray);

Output:

Array
(
    [apple] => apple
    [banana] => banana
    [cherry] => cherry
)
  1. Change the key value of the array

We can use The array_map function, combined with an anonymous function, changes the key value of the array. The anonymous function passes each value as a parameter and returns a new value, then stores these new key-value pairs in a new array.

Here is an example:

$oldArray = array(
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'cherry'
);

$newArray = array_map(function($value) {
    return strtoupper($value);
}, $oldArray);

print_r($newArray);

Output:

Array
(
    [a] => APPLE
    [b] => BANANA
    [c] => CHERRY
)

In the above example, we use the array_map function to pass each value to a Anonymous function that converts a string to uppercase letters and stores the new key-value pairs in a new array.

  1. Change the key name of a multi-dimensional array

If you have a multi-dimensional array, you can use recursion to change the key name. We can easily convert multidimensional arrays by recursively iterating through each value and changing its key name.

The following is an example:

$array = array(
    "a" => array(
        "b" => array(
            "c" => "apple"
        )
    ),
    "d" => array(
        "e" => array(
            "f" => "banana"
        )
    ),
    "g" => array(
        "h" => "cherry"
    )
);

function changeKeys(array $array, array $newKeys): array
{
    $newArray = array();
    foreach ($array as $key => $value) {
        if (array_key_exists($key, $newKeys)) {
            $key = $newKeys[$key];
        }
        if (is_array($value)) {
            $value = changeKeys($value, $newKeys);
        }
        $newArray[$key] = $value;
    }
    return $newArray;
}

$newKeys = array(
    'a' => 'apple',
    'd' => 'banana',
    'g' => 'cherry'
);

$newArray = changeKeys($array, $newKeys);

print_r($newArray);

Output:

Array
(
    [apple] => Array
        (
            [b] => Array
                (
                    [c] => apple
                )

        )

    [banana] => Array
        (
            [e] => Array
                (
                    [f] => banana
                )

        )

    [cherry] => Array
        (
            [h] => cherry
        )

)

In the above example, we recursively traverse the array and find the corresponding item of the new key name, and then use Make a new key name. If nested arrays are encountered, modifications continue during the recursion.

Summary

In PHP, changing the key name and key value of an array is a very useful operation, especially when dealing with multi-dimensional data. This article describes three common methods: using the array_combine function to change the key name of an array, using the array_map function to change the key name of an array, and recursively changing the key name of a multidimensional array. Hope this article is helpful to you.

The above is the detailed content of How to change the keys of an 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