Home  >  Article  >  Backend Development  >  How to convert the key value of an array in php

How to convert the key value of an array in php

PHPz
PHPzOriginal
2023-04-19 10:07:07631browse

In PHP programming, array is a very common data structure. Arrays are usually composed of a set of key-value pairs, where the key is the unique identifier of the array element and the value is the data stored in the element. In practical applications, we often need to transform the key values ​​of arrays to meet different needs.

  1. Convert array key names to lowercase letters

In some cases, we need to uniformly convert the key names of the array (that is, the key of the array) into lowercase letters. For example, when receiving form data submitted by users, in order to prevent errors caused by inconsistent key names entered by users, we can convert all key names into lowercase letters.

PHP provides the array_change_key_case function, which can convert all array key names to lowercase letters. The usage of the function is as follows:

array array_change_key_case ( array $array [, int $case = CASE_LOWER ] )

The $array parameter is the array that needs to be converted, and the $case parameter specifies the converted letter case form. When $case is CASE_LOWER (the default), all key names are converted to lowercase letters; when $case is CASE_UPPER, all key names are converted to uppercase letters.

The following is a code example:

// 定义一个大小写不规范的数组
$arr = array(
    'Name' => '张三',
    'AGE' => 20,
    'hObBy' => 'running'
);

// 将数组的键名全部转换为小写字母
$arr = array_change_key_case($arr, CASE_LOWER);

// 输出转换后的数组
print_r($arr);

The above code will output the following results:

Array
(
    [name] => 张三
    [age] => 20
    [hobby] => running
)
  1. The array key name is converted to uppercase letters

Similarly, when we need to convert all the key names of the array to uppercase letters, we can use the array_change_key_case function and set the $case parameter to CASE_UPPER. For example:

// 定义一个大小写不规范的数组
$arr = array(
    'Name' => '张三',
    'AGE' => 20,
    'hObBy' => 'running'
);

// 将数组的键名全部转换为大写字母
$arr = array_change_key_case($arr, CASE_UPPER);

// 输出转换后的数组
print_r($arr);

The above code will output the following results:

Array
(
    [NAME] => 张三
    [AGE] => 20
    [HOBBY] => running
)
  1. Convert the first letter of the array key name to uppercase

Sometimes, we need to convert the array The first letter of the key name is converted to uppercase letters. For example, when we design an ORM framework similar to ActiveRecord, we need to convert database field names into corresponding model property names, and model property names usually start with a capital letter.

PHP provides a function called array_combine, which can combine two arrays into a new array, and use the elements in the first array as the keys of the new array, and the elements in the second array elements as the values ​​of the new array. Well, we can use this function to generate a new array and convert the first letter of the key name to uppercase letters.

The following is a code example:

// 定义一个数组
$arr = array(
    'Name' => '张三',
    'age' => 20,
    'hobby' => 'running'
);

// 将数组的键名的首字母转换为大写字母
$new_arr = array_combine(
    array_map('ucfirst', array_keys($arr)),
    array_values($arr)
);

// 输出转换后的数组
print_r($new_arr);

The above code will output the following results:

Array
(
    [Name] => 张三
    [Age] => 20
    [Hobby] => running
)

In the above code, we use the array_map function to map the key names of the array. operate. The array_map function accepts a callback function as a parameter, and passes each element in the array to the callback function for processing. In this example, we used the built-in function ucfirst to convert the first letter of the string to uppercase.

  1. Exchange the key name and key value of the array

Sometimes, we need to exchange the key name and key value of the array, that is, the original value will be used as the key value of the new array Key name, use the original key name as the value of the new array. This conversion is very common when processing data and allows us to quickly search and filter.

PHP provides a function called array_flip, which can interchange the key names and key values ​​of the array. The usage of the function is as follows:

array array_flip ( array $array )

The $array parameter is the array that needs to be converted.

The following is a code example:

// 定义一个数组
$arr = array(
    'Name' => '张三',
    'Age' => 20,
    'Hobby' => 'running'
);

// 将数组的键名和键值互换
$new_arr = array_flip($arr);

// 输出转换后的数组
print_r($new_arr);

The above code will output the following results:

Array
(
    [张三] => Name
    [20] => Age
    [running] => Hobby
)

In the above code, the array_flip function swaps the key names and key values ​​​​in the original array. Replace, thereby generating a new array.

Summary

In PHP programming, array key-value conversion is one of the most common operations. Through some built-in functions provided by PHP, we can easily convert the key values ​​of the array to meet different needs. In actual development, we should choose the appropriate conversion method according to the specific situation to achieve better development results.

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