How to change key value in php array through key name

PHPz
Release: 2023-04-18 14:40:13
Original
470 people have browsed it

In PHP, array is one of the most commonly used data types. It refers to a collection of related variables. Each array element can be accessed and manipulated through its corresponding key name, which can be an integer or a string type. In some cases, we need to modify the key value in the array through the key name. Here we will introduce how to achieve it.

PHP provides some built-in functions to implement methods of modifying array elements, such as array_replace(), array_replace_recursive(), but the disadvantages of these functions are : If the key name does not exist, add a new element. If this isn't what you want, it might be better to implement a function that modifies the key value yourself.

First of all, we need to know that in PHP, the key name of the array is a pointer to the key value. Therefore, we can modify the key value by operating the key name. We can use the array_keys() function to get all the key names in the array, then traverse the key names, access the key value through the current key name, and update the corresponding key value if the modification conditions are met.

The following is an implementation of a sample function change_array_key(), which traverses the input array. If the current key name is equal to the key name to be modified, the key value is modified to the new key Value:

function change_array_key($arr, $old_key, $new_value) {
    $keys = array_keys($arr);
    foreach($keys as $key) {
        if($key == $old_key) {
            $arr[$key] = $new_value;
        }
    }
    return $arr;
}
Copy after login

Using the above example function, we can easily change the key value in the array. The following is a practical example:

$arr = array("name"=>"Tom","age"=>23,"sex"=>"male");
echo "修改前:<br>";
print_r($arr);
$new_arr = change_array_key($arr, "age", 24);
echo "修改后:<br>";
print_r($new_arr);
Copy after login

The output result is:

修改前:
Array
(
    [name] => Tom
    [age] => 23
    [sex] => male
)

修改后:
Array
(
    [name] => Tom
    [age] => 24
    [sex] => male
)
Copy after login

In the above code, we first define an array $arr, and then use change_array_key () The function modifies the key value corresponding to the age key name in the array to 24. The modified array is returned through the $new_arr variable, and finally the array before and after modification is printed. output.

When a large number of array elements need to be modified, this method can be used to implement it more efficiently and avoid the problem of adding redundant elements that may occur when using built-in functions.

In short, by manipulating the key names of the array, we can easily modify the key values ​​​​in the php array. Use the array_keys() function to get all the key names in the array, then traverse the key names, and finally update the corresponding key values.

The above is the detailed content of How to change key value in php array through key name. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template