PHP array_change_key_case: Change Array Key Cases

php array_change_key_case

The array_change_key_case function in PHP changes all array keys to lowercase or uppercase for consistent data use.

Understand the array_change_key_case Function in PHP

The array_change_key_case function changes the case of keys in an array. It changes every key at once and returns a new array.

The syntax looks like this:

array_change_key_case($array, $case = CASE_LOWER)
  • $array: the input array with mixed key cases.
  • $case: optional constant. Use CASE_LOWER or CASE_UPPER.
  • Return: a new array with all keys in the case you set.

Keys in arrays often mix lowercase and uppercase. This breaks uniform access. The function fixes the issue by changing all keys to one style.

Here is a quick example:

$data = ["Name" => "Sara", "AGE" => 25];
print_r(array_change_key_case($data));

This example changes all keys to lowercase. You see name and age instead of mixed forms.

The output:

Array
(
[name] => Sara
[age] => 25
)

The function loops over every key and applies the case rule. It does not touch the values. The result is a new array with changed keys.

Lowercase conversion:

$data = ["City" => "Cairo", "COUNTRY" => "Egypt"];
print_r(array_change_key_case($data, CASE_LOWER));

This code makes all keys lowercase. The output keys are city and country.

The output:

Array
(
[city] => Cairo
[country] => Egypt
)

Uppercase conversion:

$data = ["City" => "Cairo", "COUNTRY" => "Egypt"];
print_r(array_change_key_case($data, CASE_UPPER));

The output:

Array
(
[CITY] => Cairo
[COUNTRY] => Egypt
)

This code makes all keys uppercase. The output keys are CITY and COUNTRY.

The Difference Between strtolower Keys and array_change_key_case

The strtolower function works on strings. It changes every letter in a string to lowercase. You pass one string, and you get a lowercase version of that string.

The array_change_key_case function works on arrays. It changes every key in the array. It does not touch the values.

Here is a table shows key differences:

FunctionWorks OnResult
strtolowerSingle stringLowercase string
array_change_key_caseArray keysArray with changed keys

Use strtolower when you want one string in lowercase. Use array_change_key_case when you want all keys in an array in the same case.

Examples of the array_change_key_case in PHP

Normalize User Data Keys:

$data = ["UserName" => "Ali", "Email" => "ali@test.com"];
$result = array_change_key_case($data, CASE_LOWER);
print_r($result);

This example changes mixed-case user data keys to lowercase. You can avoid a mismatch in later access.

Here is the output:

Array
(
[username] => Ali
[email] => ali@test.com
)

Match API Response Keys:

$response = ["id" => 101, "NaMe" => "Lina", "SCORE" => 89];
$fixed = array_change_key_case($response, CASE_UPPER);
print_r($fixed);

This example changes every API response key to uppercase. You match external systems that use uppercase keys. The values stay the same. The result:

Array
(
[ID] => 101
[NAME] => Lina
[SCORE] => 89
)

Combine Arrays:

$a = ["Name" => "Omar", "Age" => 22];
$b = ["NAME" => "Omar", "AGE" => 22];
$a = array_change_key_case($a, CASE_LOWER);
$b = array_change_key_case($b, CASE_LOWER);
print_r(array_merge($a, $b));

Here, it converts both arrays to lowercase before merging them and avoids key conflicts from mixed cases.

Here is the output:

Array
(
[name] => Omar
[age] => 22
)

Handle Config Data:

$config = ["Host" => "localhost", "PORT" => 3306, "User" => "root"];
$fixed = array_change_key_case($config, CASE_UPPER);
print_r($fixed);

The result:

Array
(
    [HOST] => localhost
    [PORT] => 3306
    [USER] => root
)

This changes configuration keys to uppercase.

Wrapping Up

You learned what array_change_key_case does and how it works.

Here is a quick recap:

  • It changes all array keys to lowercase or uppercase.
  • It works only on keys and not on values.
  • It avoids errors with mixed-case keys.

FAQs

What does php array_change_key_case do?

The array_change_key_case() function changes all array keys to lowercase or uppercase.
  • Syntax: array_change_key_case(array, case)
  • Case: Use CASE_LOWER or CASE_UPPER

How to convert array keys to lowercase in PHP?

Use array_change_key_case() with CASE_LOWER. Example:
$array = ["ID" => 101, "Name" => "John"];
$result = array_change_key_case($array, CASE_LOWER);
print_r($result);
Output:
Array ( [id] => 101 [name] => John )

How to convert array keys to uppercase in PHP?

Use array_change_key_case() with CASE_UPPER. Example:
$array = ["id" => 101, "name" => "John"];
$result = array_change_key_case($array, CASE_UPPER);
print_r($result);
Output:
Array ( [ID] => 101 [NAME] => John )

What is the difference between strtolower and array_change_key_case?

strtolower() works on strings. array_change_key_case() works on array keys.
  • strtolower: strtolower("HELLO") → "hello"
  • array_change_key_case: converts every key in the array

Similar Reads

Understanding the PHP Enumerable

Actually, the PHP enumerable is a new feature of 8.1 which allows you to define a new type. This feature…

PHP array_all Function: How it Works with Examples

PHP 8.4 released a new built-in function, array_all checks to check if all array values meet a condition. It gives…

PHP filter_list(): List Available Filters

PHP introduces the filter_list() function to give developers a way to check all available filters in the filter extension. This…

PHP compact Function: Assoc Array from Variables

The PHP compact function is a way to pull variables into an array. In this article, you will learn how…

Understanding the PHP Exclusive OR (XOR) Operator

In PHP, there is this interesting operator known as "exclusive OR," or just XOR. It is somewhat of an underdog…

PHP error_log Function: How It Works with Examples

Sometimes things go wrong when PHP runs the code. So, you need to know the reasons for this error to…

PHP continue Statement: How to Skip Steps in Iteration

The PHP continue statement skips the rest of the loop in the current cycle. It jumps to the next cycle…

PHP Namespace: How to Group Code (With Example)

PHP namespace solves the problem of name conflicts. Different developers may create functions-classes, or constants with the same name. PHP…

PHP array_chunk(): Split Arrays into Manageable Parts

When working with big arrays in PHP, things can get messy. That is where php array_chunk() steps in by providing…

PHP SimpleXML: Work with XML in Examples

PHP SimpleXML - invented for you to work with XML. Way leaner, quicker to get up. it's already integrated directly…

Previous Article

HTML Element: Everything You Need To Know with Examples

Next Article

SQL Server Architecture: Components & Functions

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.