PHP array change key case
Last Updated on Feb 14, 2023
Sometimes you might change the case of the keys in your array. Maybe you want to have consistency of data, maybe you want to convert it to json and you want it to be the same format.
For whatever reason, it does not matter, it’s very easy to do that.
Array Change Key Case
You can pass your array to array_change_key_case
The first argument is your array
And the second argument is CASE_UPPER or CASE_LOWER. Default is CASE_LOWER
It’s a very useful function especially when you load your data to an associative array from another source and the keys are not consistent.
In an associative array you should get the value by key and if the key is not consistent in being lower or uppercase it would be annoying to remember all of that.
We can easily use that function and fix it
Let’s see an example
$array = ["twiTteR"=> 63, "PrathUm" => 891, "AmiR" => 47]; array_change_key_case($array, CASE_UPPER); // Array ( // [TWITTER] => 63 // [PRATHUM] => 891 // [AMIR] => 47 // )
Now you know about array change key case in PHP.
I recommend you to open a PHP files define arrays with different key cases. Then try to use this function to change all of them to lower or upper case.
If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!
- array change key case in php
- lower case arrays keys
- upper case array keys