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

How to change the key value of an array in php

PHPz
PHPzOriginal
2023-04-18 10:25:41663browse

In PHP, an array is a very powerful data structure that can store and access large amounts of data quickly. During the development process, we often need to generate a new array from an array and modify the key value of each element of the original array. At this time, you need to use some functions of PHP to change the key value of the array.

PHP provides many methods to change the key value of the array. Here are some commonly used methods.

1. Use the array_combine function

The array_combine function is a built-in function in PHP. It can be used to use the key name of an array as the key name of a new array and the value of the array as the key name of the new array. value. The syntax of this function is as follows:

array_combine(array $keys , array $values ) : array

This function has two parameters, which are the original key name array of the array that needs to be modified and the modified key name array. If the numbers of the two arrays are inconsistent, false is returned; otherwise, a new array is returned.

For example:

$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange");
$newKeys = array("a", "b", "c");
$newFruits = array_combine($newKeys, $fruits);
print_r($newFruits);

Execution result:

Array
(
    [a] => red
    [b] => yellow
    [c] => orange
)

In the above code, we use the key name of the $fruits array as the value of the new array, and $newKeys as the new The key name of the array generates the $newFruits array.

2. Use the array_walk function

The array_walk function is also one of PHP's built-in functions. It can traverse each element in the array and call a custom callback function to change the key name.

The syntax of this function is as follows:

array_walk(array &$array , callable $callback [, mixed $userdata = NULL ] ) : bool

The first parameter of this function is the array that needs to be modified, and the second parameter is a custom callback function, which is used to modify the original array. key name in . The third parameter is optional user data.

For example:

$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange");
function changeKey(&$value, $key, $prefix) {
    $value = $prefix . $key;
}
array_walk($fruits, 'changeKey', 'fruit_');
print_r($fruits);

Execution result:

Array
(
    [fruit_apple] => red
    [fruit_banana] => yellow
    [fruit_orange] => orange
)

In the above code, we define a custom callback function changeKey, which receives three parameters: A reference to the array element, the element's key name and user data. In this callback function, we add the prefix "fruit_" to the original key name.

3. Use array_map function

The array_map function is also one of the array methods. It can accept multiple arrays as parameters and pass them to a callback function. In the callback function, we can modify the keys in the original array to generate a new array.

The syntax of this function is as follows:

array_map(callable $callback , array $array1 [, array $array2, ... ] ) : array

The first parameter of this function is the callback function. This function will receive the elements of the multiple arrays mentioned above as parameters and return a new array. Note that all parameter arrays must have the same number of elements.

For example:

$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange");
$keysSuffix = array("1", "2", "3");
$newKeys = array_map(function($key, $suffix) {
    return $key . $suffix;
}, array_keys($fruits), $keysSuffix);
$newFruits = array_combine($newKeys, $fruits);
print_r($newFruits);

Execution result:

Array
(
    [apple1] => red
    [banana2] => yellow
    [orange3] => orange
)

In the above code, we use the array_keys function to obtain the key name of the original array, and use the array_map function to Add the prefix suffix to the original key name. Finally, we use the array_combine function to combine the new key name and the original value into a new array.

To sum up, we can use multiple methods to change array key values ​​in PHP, each method has its own advantages and disadvantages. When using it, we need to choose the most appropriate method to achieve our needs based on the actual situation.

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