Current Location: Home> Latest Articles> How to use arrays in combination with array_change_key_case(), array_keys() and array_values()?

How to use arrays in combination with array_change_key_case(), array_keys() and array_values()?

M66 2025-04-24

In PHP, arrays are very flexible data structures. In our daily development, we often need to process the key names or key values ​​of arrays. Today we will take a look at three very commonly used array functions - array_change_key_case() , array_keys() and array_values() , and learn how to combine them to achieve complex data processing requirements.

1. array_change_key_case()

This function is used to convert all key names in an array to lowercase or uppercase. Its basic syntax is as follows:

 array_change_key_case(array $array, int $case = CASE_LOWER): array

Parameter description:

  • $array : The input array.

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

Example:

 $data = [
    "Name" => "Alice",
    "AGE" => 25,
    "Email" => "alice@m66.net"
];

$lowerKeys = array_change_key_case($data, CASE_LOWER);
print_r($lowerKeys);

Output:

 Array
(
    [name] => Alice
    [age] => 25
    [email] => alice@vv99.net
)

2. array_keys() and array_values()

These two functions are used to extract all key names and all key values ​​of the array respectively.

 array_keys(array $array): array
array_values(array $array): array

Example:

 $data = [
    "name" => "Alice",
    "age" => 25,
    "email" => "alice@m66.net"
];

$keys = array_keys($data);
$values = array_values($data);

print_r($keys);
print_r($values);

Output:

 Array
(
    [0] => name
    [1] => age
    [2] => email
)
Array
(
    [0] => Alice
    [1] => 25
    [2] => alice@vv99.net
)

Three, three functions combined

Sometimes the data format we receive from different sources (such as APIs or forms) is not uniform, such as inconsistent case of key names. We can first use array_change_key_case() to unify the key names, and then use array_keys() and array_values() to extract the keys and values ​​respectively for further processing or mapping.

Sample Scenario: Standardize user information data

 $userData = [
    "NaMe" => "Bob",
    "AgE" => 30,
    "EMAIL" => "bob@m66.net"
];

// step 1:The unified key name is lowercase
$normalized = array_change_key_case($userData, CASE_LOWER);

// step 2:Extract key names and key values
$keys = array_keys($normalized);
$values = array_values($normalized);

// Print to view results
print_r($keys);
print_r($values);

Output:

 Array
(
    [0] => name
    [1] => age
    [2] => email
)
Array
(
    [0] => Bob
    [1] => 30
    [2] => bob@vv99.net
)

4. Advanced usage: recombining arrays

If you want to recombinate the array based on processed keys and values, you can use array_combine() :

 $cleanedData = array_combine($keys, $values);
print_r($cleanedData);

The output is still an array with unified structure and clear key values:

 Array
(
    [name] => Bob
    [age] => 30
    [email] => bob@vv99.net
)