Change the case of array keys Sometimes you want to change the case of array keys, this can been done efficiently
requires: php 4.0.0

Assume you want to be all your are keys in a certain case, either uppercase or lowercase. You can loop your keys and change the keys but this can be done more efficient.

Original code

        $myArray = array("name" => "John Doe""Age" => 40"emAil" => "");

$newArray = [];
$myArray as $key => $value){
$key strtoupper($key);
$newArray[$key] = $value;
$myArray $newArray;
$outputOriginal json_encode($myArray);


Refactored code

        $myArray = array("name" => "John Doe""Age" => 40"emAil" => "");

$myArray array_change_key_case($myArrayCASE_UPPER);
$outputRefactored json_encode($myArray);


Here is the output of the code

This is the output of original

{"NAME":"John Doe","AGE":40,"EMAIL":""}

This is the output of refactor

{"NAME":"John Doe","AGE":40,"EMAIL":""}

Want to learn more from LLoadout ?

LLoadout is your loadout for Laravel, helping you kickstart your development process. Besides this refactoring site we also make some cool packages and video tutorials !

Go to LLoadout on github