Home >Backend Development >PHP Problem >php change the key of the array

php change the key of the array

王林
王林Original
2023-05-07 15:31:10506browse

In PHP, you can use the array_keys() function to get all the keys of the array, and the array_values() function to get all the values ​​of the array. Through these two functions, you can easily change the array key name.

Method 1: Use the array_combine() function

array_combine() function uses the key name of an array as the value of the new array, and uses the key value of an array as the key name of the new array. Therefore, you can first use the array_keys() and array_values() functions to obtain the key names and key values ​​of the original array, then use the array_combine() function to convert the key names and key values ​​into a new array, and finally use the unset() function to delete the original array. The following is a sample code:

// 原数组
$old_array = array(
    'id' => 1,
    'name' => 'Tom',
    'age' => 20,
);
// 获取原数组的键名和键值
$keys = array_keys($old_array);
$values = array_values($old_array);
// 将键名和键值转换成新数组
$new_array = array_combine($values, $keys);
// 删除原数组
unset($old_array);
// 输出新数组
print_r($new_array);

Execute the above code, the output result is:

Array
(
    [1] => id
    [Tom] => name
    [20] => age
)

The above code uses the key name of the original array as the value of the new array, and uses the key value of the original array as the new The key name of the array, successfully implemented the operation of changing the key name of the array.

Method 2: Use the array_flip() function

array_flip() function uses the key name of the array as the value of the new array, and uses the key value of the array as the key name of the new array. Therefore, you can directly use the array_flip() function to exchange the key names and key values ​​of the original array, and finally use the unset() function to delete the original array.

// 原数组
$old_array = array(
    'id' => 1,
    'name' => 'Tom',
    'age' => 20,
);
// 将键名和键值交换
$new_array = array_flip($old_array);
// 删除原数组
unset($old_array);
// 输出新数组
print_r($new_array);

Execute the above code, and the output result is:

Array
(
    [1] => id
    [Tom] => name
    [20] => age
)

The above code directly uses the array_flip() function to exchange the key name and key value of the original array, successfully realizing the operation of changing the array key name. .

Whether you use the array_combine() function or the array_flip() function, you can easily change the array key name. Just choose the appropriate method according to the actual situation to make the code more concise and elegant.

The above is the detailed content of php change the key of the array. 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