Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
    }
}
share|improve this question
 
why have you used & in line 3 of function fixArrayKeys? –  Abhishek Bhatia Nov 15 '12 at 5:17
 
I think you have to pass the whole array by reference not just key.. –  Dinesh Nov 15 '12 at 5:19
add comment

3 Answers

up vote 4 down vote accepted
function fixArrayKey(&$arr)
{
    $arr=array_combine(array_map(function($str){return str_replace(" ","_",$str);},array_keys($arr)),array_values($arr));
    foreach($arr as $key=>$val)
    {
        if(is_array($val)) fixArrayKey($arr[$key]);
    }
}

Tested as below:

$data=array("key 1"=>"abc","key 2"=>array("sub 1"=>"abc","sub 2"=>"def"),"key 3"=>"ghi");
print_r($data);
fixArrayKey($data);
print_r($data);

This outputs:

Array
(
    [key 1] => abc
    [key 2] => Array
        (
            [sub 1] => abc
            [sub 2] => def
        )

    [key 3] => ghi
)
Array
(
    [key_1] => abc
    [key_2] => Array
        (
            [sub_1] => abc
            [sub_2] => def
        )

    [key_3] => ghi
)
share|improve this answer
add comment

Why don't you remove the spaces from key at the first place

foreach ($all_regions as $key => $value){
      $key = strtolower(str_replace(' ', '_', $key));
      $all_regions[$key] = strtolower(str_replace(' ', '_', $value));
}
share|improve this answer
 
Because I have no control over the building of the array. That snippet I posted was another users code to use as reference –  Bankzilla Nov 15 '12 at 6:05
add comment

Try Below, have not tested it, but see the key differences

function fixArrayKeys(&$array)
{
    if(is_array($array)){
        foreach($array as &$key => $value){
            if(!is_array($value))
                $array[strtolower(str_replace(' ', '_', $key))] = $value;
            else
                fixArrayKeys(&$value);
        }
    } else {
        return $array;
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.