I have an array with spaces all throughout the keys. This makes a problem for targeting in other programs which can't target spaces and is bad practice to have spaces in keys.
I'm looking for something that will remove the key spaces and replace with underscores in a multidimensional array. Most likely would have to be a recursive function?
Found something similar in another question but was about replacing in values.
foreach ($all_regions as $key => $value){
$all_regions[$key] = strtolower(str_replace(' ', '_', $value));
}
Pretty much need this replicated but for keys. The problem I'm hitting is that I can think of how to make reference to the key itself, because if you try push like the above method it would just recreate another key with underscores.
A snippet of the array, this is as deep as it goes.
Array
(
[0] => Array
(
[Line Identifier] => PID
[Set ID] => 1
[User ID] =>
[Requests] => Array
(
[0] => Array
(
[Line Identifier] => OBR
[Set ID] => 1
[Placer Order Number] => 021120091525
[Results] => Array
(
[0] => Array
(
[Line Identifier] => OBX
[Set ID] => 1
[1] => Array
(
[Line Identifier] => OBX
[Set ID] => 2
I've tried the below, but Key element cannot be a reference
private function fixArrayKeys($array){
if(is_array($array)){
foreach($array as &$key => $value){
if(!is_array($key))
$array[strtolower(str_replace(' ', '_', $key))] = $value;
else
fixArrayKeys($array);
}
} else {
return $array;
}
}
&
in line 3 of functionfixArrayKeys
? – Abhishek Bhatia Nov 15 '12 at 5:17