PHP array_change_key_case() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_change_key_case() returns a copy of an array with all string keys changed to lowercase or uppercase. The values are not touched.

You use array_change_key_case() to make keys consistent, for example when data comes from forms, headers or CSV files with mixed capital letters. After the change you can read 'name' without worrying about whether the source wrote 'Name' or 'NAME'.

Syntax

Call array_change_key_case() with the values you need. Start with this line.

PHP
$clean = array_change_key_case($input, CASE_LOWER);
CallWhat it does
array_change_key_case($user)Returns a copy with all string keys in lowercase, because CASE_LOWER is the default.
array_change_key_case($user, CASE_UPPER)Returns a copy with all string keys in uppercase.
array_change_key_case([10 => 'x'])Returns the same array, because integer keys are never changed.

The original array is not modified. The function returns a new array, so store the result. Only the keys change, and only string keys are affected. Values, including nested arrays, are left exactly as they were.

Library declarations
PHP
array_change_key_case(array $array, int $case = CASE_LOWER): array

Parameters

ParameterDescription
arrayRequired. The array whose keys should be changed.
caseOptional. Use CASE_LOWER (the default) for lowercase keys or CASE_UPPER for uppercase keys.

Return Value

Return ValueDescription
arrayA new array with the same values and with string keys in the chosen case.

PHP Examples

Example 1 — Lowercase and Uppercase Keys

By default the keys become lowercase. Pass CASE_UPPER to make them uppercase.

PHP
<?php
$user = ['Name' => 'Asha', 'CITY' => 'Pune'];

print_r(array_change_key_case($user));
print_r(array_change_key_case($user, CASE_UPPER));

How It Works

  1. $user has the mixed case keys 'Name' and 'CITY'.
  2. The first call uses the default CASE_LOWER, so the keys become 'name' and 'city'.
  3. The second call passes CASE_UPPER, so the keys become 'NAME' and 'CITY'.
  4. The values 'Asha' and 'Pune' stay the same in both results.

Example 2 — Keys That Collide

Different keys can become identical after the change. Only the later value is kept.

PHP
<?php
$data = ['a' => 1, 'A' => 2, 'b' => 3];

print_r(array_change_key_case($data));

How It Works

  1. 'a' and 'A' are different keys in the original array.
  2. After lowercasing, both would be 'a', so they collide.
  3. The later item 'A' => 2 overwrites the earlier 'a' => 1.
  4. The result has only two items and the value 1 is lost.

Example 3 — Integer Keys Are Left Alone

Only string keys are changed. Integer keys stay as they are.

PHP
<?php
$data = [10 => 'x', 'Key' => 'y', 'Other' => 'z'];

print_r(array_change_key_case($data, CASE_UPPER));

How It Works

  1. The array has one integer key, 10, and two string keys.
  2. CASE_UPPER changes the string keys 'Key' and 'Other' to uppercase.
  3. The integer key 10 has no letters, so it is not changed.
  4. The order of the items and all of the values stay the same.

Frequently Asked Questions

It returns a copy of an array where all string keys are lowercase or uppercase. The values and the order of the items stay the same.
No. It returns a new array and leaves the original untouched. Save the return value in a variable to use it.
Only one item is kept. The one that appears later in the array overwrites the earlier one. No warning is shown, so check your data if keys might collide.
No. Only the keys on the first level change. To change nested keys you must call the function again on each inner array, for example with array_map().
They are built in constants. CASE_LOWER (value 0) makes keys lowercase and is the default. CASE_UPPER (value 1) makes keys uppercase.

Next: array_chunk()

array_chunk() splits an array into smaller arrays of a fixed size. It returns a new array .

array_chunk() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful