Current Location: Home> Latest Articles> How to combine array_combine() to perform case conversion and refactor key-value pairs?

How to combine array_combine() to perform case conversion and refactor key-value pairs?

M66 2025-04-24

In PHP, when processing arrays, you often encounter situations where key names are inconsistent or require a unified format. array_change_key_case() is a very practical function that allows us to quickly convert all key names of an array to uppercase or lowercase. array_combine() can recombine two arrays into a new array corresponding to a key value.

This article will introduce how to combine these two functions to not only unify the case of key names, but also reorganize the array structure.

1. Function introduction

array_change_key_case()

This function is used to convert all key names of an array to lowercase or uppercase.

 array_change_key_case(array $array, int $case = CASE_LOWER): array
  • $array : The array to be processed.

  • $case : Target case, optional value is CASE_LOWER (default) or CASE_UPPER .

array_combine()

Used to combine two numbers into an array, one as the key name and the other as the corresponding value.

 array_combine(array $keys, array $values): array
  • $keys : will be the key name of the new array.

  • $values : will be the value of the new array.

  • Note: Both arrays must have the same number of elements.

2. Practical application scenarios

Suppose we have two arrays: one is an array of key names and the other is an array of values. We want to unify the key name in lowercase and then combine it with the value and into a new array.

Sample code:

 <?php

// Original array of key names(Case mix)
$keys = ['Name', 'EMAIL', 'Age'];

// Corresponding value array
$values = ['Alice', 'alice@m66.net', 30];

// Convert key names to lowercase
$lowercaseKeys = array_change_key_case(array_combine($keys, $values), CASE_LOWER);

// Output processed array
print_r($lowercaseKeys);

Output result:

 Array
(
    [name] => Alice
    [email] => alice@m66.net
    [age] => 30
)

3. Why use it like this?

  1. Unity : In many cases, the array key names returned by the interface may not be uniform (for example, some are lowercase and some are uppercase). Using array_change_key_case() can improve the consistency of data processing.

  2. Readability : The unified format makes the code easier to read and reduces maintenance costs.

  3. Security : Some functions or libraries are sensitive to key names when processing arrays, and inconsistent case can lead to unexpected errors.

4. Things to note

  • array_change_key_case() only works on one-dimensional arrays, and recursively processing is required.

  • Make sure that the two arrays are the same length before using array_combine() , otherwise false will be returned.

  • If you already have a key-value pair array, you just need to unify the case of the key name, just use array_change_key_case() , no need for array_combine() .

5. Advanced: Processing user form data

A practical application scenario is the form data submitted by a user, and its field names may be case inconsistent due to manual input. We can first extract the field name array and the corresponding value array, and then store or verify it after the key name format.

 <?php

// Assume fields and values ​​from a form
$formKeys = ['UserName', 'EMail', 'Phone'];
$formValues = ['bob', 'bob@m66.net', '123456789'];

// Combine and unify the key names
$processedForm = array_change_key_case(array_combine($formKeys, $formValues), CASE_LOWER);

// result
print_r($processedForm);

6. Conclusion

By combining array_change_key_case() and array_combine() , we can gracefully complete the format uniformity of array key names and the reorganization of data structures. This method is very useful in handling external data input, interface response conversion, array normalization and other scenarios. Mastering this technique can significantly improve your PHP array operation capabilities.