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:
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.
$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]
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.