How to Change All Keys of a PHP Array to Lowercase?

Find out how to convert all keys of an array in PHP to lowercase

You can simply use the array_change_key_case() function in PHP to change the case of all keys in an array. To use this function to change all keys to lowercase, you can specify CASE_LOWER constant as the second argument to the function, for example, like so:

array_change_key_case($array, CASE_LOWER);

The second argument to array_change_key_case() defaults to CASE_LOWER. Therefore, you don't need to specify it explicitly.

If you array contains numeric keys as well, then they're left as is.

For example:

$arr = [
    'Foo' => 1,
    'BAR' => 2,
    'baz' => 3,
    'QuX' => 4,
];

$arrLowercaseKeys = array_change_key_case($arr, CASE_LOWER);

var_dump($arrLowercaseKeys); // ['foo' => 1, 'bar' => 2, 'baz' => 3, 'qux' => 4]

Hope you found this post useful. It was published . Please show your love and support by sharing this post.