In PHP, array_change_key_case() and array_map() are both very practical array processing functions, but they perform differently when dealing with multidimensional arrays . So, can these two functions be combined to process multi-dimensional arrays? Let's discuss this issue together.
array_change_key_case(array $array, int $case = CASE_LOWER) : Used to convert all key names in an array to lowercase or uppercase.
array_map(callable $callback, array $array) : Apply a callback function to each element in the array.
The use of these two functions is stress-free when processing one-dimensional arrays, but when it comes to the multi-dimensional array scenario, some additional logical processing is required.
Suppose we have a multidimensional array as follows:
$data = [
"Name" => "Alice",
"Details" => [
"Age" => 25,
"Email" => "alice@m66.net"
],
"Preferences" => [
"Colors" => ["Red", "Green"],
"Languages" => ["PHP", "JavaScript"]
]
];
We want to convert all key names to lowercase. At this time, if you use array_change_key_case() directly:
$result = array_change_key_case($data, CASE_LOWER);
We can only handle the outermost keys, and the keys in Details and Preferences will not be converted.
This leads to the core question of this article: How to use the combination of array_change_key_case() and array_map() to recursively process multidimensional arrays?
We can customize a recursive function, combining array_change_key_case() to handle keys at all levels:
function recursive_change_key_case(array $array, int $case = CASE_LOWER): array {
$newArray = array_change_key_case($array, $case);
foreach ($newArray as $key => $value) {
if (is_array($value)) {
$newArray[$key] = recursive_change_key_case($value, $case);
}
}
return $newArray;
}
Called:
$processed = recursive_change_key_case($data, CASE_LOWER);
print_r($processed);
Array
(
[name] => Alice
[details] => Array
(
[age] => 25
[email] => alice@m66.net
)
[preferences] => Array
(
[colors] => Array
(
[0] => Red
[1] => Green
)
[languages] => Array
(
[0] => PHP
[1] => JavaScript
)
)
)
As you can see, all key names, including those at the nested level, have also been successfully converted to lowercase .
So can array_map() be used here? It is mainly suitable for processing values , not keys. If we want to process each value (rather than the key name), such as removing spaces in strings, formatting, etc., we can use it in combination:
function recursive_map(array $array, callable $callback): array {
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = recursive_map($value, $callback);
} else {
$array[$key] = $callback($value);
}
}
return $array;
}
For example, remove spaces from all strings:
$cleaned = recursive_map($processed, function($value) {
return is_string($value) ? str_replace(' ', '', $value) : $value;
});
array_change_key_case() can convert key names into upper and lower case, but only supports one-dimensional arrays ;
array_map() is more suitable for processing values in arrays and does not process keys;
To handle key name case conversion of multidimensional arrays , you can combine recursive custom functions to implement it;
If you want to deal with keys and values at the same time, the two functions can be used together and act on different goals respectively.
Mastering these techniques will greatly improve your ability to handle complex arrays in PHP. Hope this article helps you!