Home >Backend Development >PHP Problem >php array key name replacement

php array key name replacement

PHPz
PHPzOriginal
2023-05-06 13:24:10510browse

In PHP programming, array is a very commonly used data type, which allows us to store multiple related values ​​and access these values ​​with a specified key name. However, in the actual development process, we sometimes need to replace some key names in the array with other values. This article will introduce how to use PHP to achieve this function.

Suppose there is an array containing multiple key-value pairs, as shown below:

$students = array(
    '001' => '张三',
    '002' => '李四',
    '003' => '王五',
    '004' => '赵六'
);

Now we need to replace some of the key names with other values, such as changing the key name to Replace the element 001 with 101 and replace the element with key 002 with 102. We can use the array_combine function in PHP to implement key name replacement.

array_combine The function accepts two arrays as parameters, the first array is a key array, and the second array is a key value array. It will use the value in the first array as the key of the new array and the value in the second array as the key of the new array, thereby generating a new associative array.

Therefore, we can first create a new array and store the key names that need to be replaced and the new key names into it, as shown below:

$replace_keys = array(
    '001' => '101',
    '002' => '102'
);

Next, we can use array_combine Function to generate a new array containing new key names and original key values. The code is as follows:

$new_keys = array_combine($replace_keys, $students);

This function will replace the element with the specified key name in the $students array with the new key name in $replace_keys, generating a New associative array$new_keys.

Finally, we can use the array_replace function to replace the elements in the new associative array $new_keys back to the original array $students. The code is as follows:

$students = array_replace($students, $new_keys);

This function will replace the elements in the new associative array $new_keys back to the original array $students to complete the array key name replace.

The complete code is as follows:

$students = array(
    '001' => '张三',
    '002' => '李四',
    '003' => '王五',
    '004' => '赵六'
);

$replace_keys = array(
    '001' => '101',
    '002' => '102'
);

$new_keys = array_combine($replace_keys, $students);

$students = array_replace($students, $new_keys);

print_r($students);

Run the above code, the output result is as follows:

Array
(
    [101] => 张三
    [102] => 李四
    [003] => 王五
    [004] => 赵六
)

It can be seen from the output result that the elements in the original array have been Successfully replaced with the new key name. Using this method, we can quickly and easily replace the key names in the array, which improves development efficiency.

In short, the array in PHP is a powerful and flexible data type, and the two functions array_combine and array_replace provide us with an efficient and flexible Simple way to replace keys in an array. I hope the introduction in this article can help everyone understand and use PHP arrays.

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