array_change_key_case
array_change_key_case(array $array, int $case = CASE_LOWER): array
Converts all array keys to lowercase or uppercase (CASE_LOWER/CASE_UPPER); the dedicated tool for normalizing HTTP headers and config keys.
C C23
#include <ctype.h>
#include <stdio.h>
#include <string.h>
/* C strings are arrays: lowercasing a key means writing each char */
static void lower_key(char *k) {
for (int i = 0; k[i]; i++) k[i] = (char)tolower((unsigned char)k[i]);
}
int main(void) {
char key[] = "Content-Type";
lower_key(key);
printf("%s\n", key); /* content-type */
return 0;
}
content-type
C++ C++26
#include <algorithm>
#include <cctype>
#include <iostream>
#include <map>
#include <string>
static std::string lowered(std::string k) {
std::transform(k.begin(), k.end(), k.begin(),
[](unsigned char c) { return std::tolower(c); });
return k;
}
int main() {
std::map<std::string, std::string> m = {
{ "Content-Type", "text/html" },
{ "X_REQUEST_ID", "a1" }
};
// rebuild with normalized keys
std::map<std::string, std::string> out;
for (const auto& [k, v] : m) out[lowered(k)] = v;
std::cout << out.at("content-type") << "\n";
std::cout << out.at("x_request_id") << "\n";
return 0;
}
text/html a1
C# v11.0.100
using System.Collections.Generic;
using System.Linq;
class MainClass {
static void Main() {
var headers = new Dictionary<string, string> {
["Content-Type"] = "text/html",
["X_REQUEST_ID"] = "a1"
};
// ToDictionary rebuilds with normalized keys; Invariant keeps it locale-free
var lower = headers.ToDictionary(
kv => kv.Key.ToLowerInvariant(), kv => kv.Value);
System.Console.WriteLine(lower["content-type"]);
System.Console.WriteLine(lower["x_request_id"]);
}
}
text/html a1
Go v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
headers := map[string]string{
"Content-Type": "text/html",
"X_REQUEST_ID": "a1",
}
// rebuild the map with lowercased keys; lookups below need no order
lower := make(map[string]string, len(headers))
for k, v := range headers {
lower[strings.ToLower(k)] = v
}
fmt.Println(lower["content-type"])
fmt.Println(lower["x_request_id"])
}
text/html a1
Java v27.0.1
import java.util.LinkedHashMap;
import java.util.Map;
class Main {
public static void main(String[] args) {
Map<String, String> headers = new LinkedHashMap<>();
headers.put("Content-Type", "text/html");
headers.put("X_REQUEST_ID", "a1");
// Locale.ROOT keeps toLowerCase deterministic across machines
Map<String, String> lower = new LinkedHashMap<>();
headers.forEach((k, v) -> lower.put(k.toLowerCase(java.util.Locale.ROOT), v));
System.out.println(lower.get("content-type"));
System.out.println(lower.get("x_request_id"));
}
}
text/html a1
JavaScript v26.13.1
const headers = { "Content-Type": "text/html", X_REQUEST_ID: "a1" };
// fromEntries + map: the idiomatic rebuild-with-normalized-keys
const lower = Object.fromEntries(
Object.entries(headers).map(([k, v]) => [k.toLowerCase(), v])
);
console.log(lower["content-type"]);
console.log(lower["x_request_id"]);
text/html a1
PHP v8.6.0
$headers = ["Content-Type" => "text/html", "X_REQUEST_ID" => "a1", "Accept" => "*/*"];
echo json_encode(array_change_key_case($headers, CASE_LOWER)), "\n";
echo json_encode(array_change_key_case($headers, CASE_UPPER)), "\n";
// default is CASE_LOWER
echo json_encode(array_change_key_case($headers)), "\n";
{"content-type":"text\/html","x_request_id":"a1","accept":"*\/*"}
{"CONTENT-TYPE":"text\/html","X_REQUEST_ID":"a1","ACCEPT":"*\/*"}
{"content-type":"text\/html","x_request_id":"a1","accept":"*\/*"}
Python v3.15.2
headers = {"Content-Type": "text/html", "X_REQUEST_ID": "a1"}
# a dict comprehension rebuilds with normalized keys in one pass
lower = {k.lower(): v for k, v in headers.items()}
print(lower["content-type"])
print(lower["x_request_id"])
text/html a1
Rust v1.99.1
use std::collections::HashMap;
fn main() {
let mut headers = HashMap::new();
headers.insert("Content-Type", "text/html");
headers.insert("X_REQUEST_ID", "a1");
// map + collect rebuilds the table with normalized keys
let lower: HashMap<String, &str> = headers
.iter()
.map(|(k, v)| (k.to_lowercase(), *v))
.collect();
println!("{}", lower["content-type"]);
println!("{}", lower["x_request_id"]);
}
text/html a1
TypeScript v26.13.1
const headers: Record<string, string> = {
"Content-Type": "text/html",
X_REQUEST_ID: "a1",
};
// same runtime shape as JS, typed as Record
const lower: Record<string, string> = Object.fromEntries(
Object.entries(headers).map(([k, v]) => [k.toLowerCase(), v])
);
console.log(lower["content-type"]);
console.log(lower["x_request_id"]);
text/html a1
Language differences
C: keys are string arrays (the example lowers in place, char by char); pass unsigned char to tolower to avoid UB; "rebuilding a key map" in C needs a hand-written table - the example shows a single key.
C++: transform + map rebuild (example); the steadier C++ approach is a std::map with a case-insensitive comparator - lookups normalize without rebuilding.
C#: ToDictionary(kv => kv.Key.ToLowerInvariant(), ...) (example); always ToLowerInvariant() over ToLower() - the latter drifts with locale (the Turkish i problem); the Dictionary constructor accepts StringComparer.OrdinalIgnoreCase for normalize-once lookup.
Go: hand loop + strings.ToLower (example); the standard net/http Header type ships CanonicalMIMEHeaderKey (capitalize first letter of each word), more common than full lowercase for HTTP; map iteration order is random - the example's lookups do not depend on it.
Java: forEach + toLowerCase(Locale.ROOT) (example); the Locale is mandatory - the no-arg toLowerCase() drifts with the default locale; TreeMap(String.CASE_INSENSITIVE_ORDER) is the normalize-at-lookup route.
JavaScript: Object.fromEntries(entries.map(...)) is the idiomatic shape (example); PITFALL: if two keys normalize equal (X-Tag and x-tag coexisting), the later silently overwrites with no error; the fetch Headers API provides case-insensitive get out of the box.
PHP: CASE_LOWER is the default, CASE_UPPER explicit (example); numeric keys untouched; COLLISIONS MERGE SILENTLY, last writer wins - ["A"=>1,"a"=>2] collapses to one entry with the later-traversed value; the HTTP companion functions are getallheaders()/$_SERVER key normalization.
Python: a dict comprehension {k.lower(): v ...} (example); collisions likewise silently overwrite; str.casefold() is more aggressive than lower() (German ß to ss) - beware when normalizing text.
Rust: iter().map(...).collect() (example); to_lowercase() is full Unicode lowercase; on collect, colliding keys follow HashMap semantics - later overwrites; normalize-at-insert keeps a HashMap<String, V> consistent.
TypeScript: same shape as JS (example); Record<string, string> cannot express key normalization at the type level - the logic is a runtime convention and needs documenting.