function convert_to_key_case($data, $case){ $condition = ($case == 'lowercase')? 'lowercase' : 'uppercase'; $test = []; foreach($data as $key => $value) { if ($condition == 'lowercase') { $test[strtolower($key)] = $value; } else { $test[strtoupper($key)] = $value; } } return $test; } $input = ['test1'=>1, 'Test2' => 2, 'TeSt3' => 3]; print_r(convert_to_key_case($input, 'uppercase')); print_r(convert_to_key_case($input, 'lowercase')); OUTPUT Array ( [TEST1] => 1 [TEST2] => 2 [TEST3] => 3 ) Array ( [test1] => 1 [test2] => 2 [test3] => 3 )