2

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;
    }
}
2
  • why have you used & in line 3 of function fixArrayKeys? Commented Nov 15, 2012 at 5:17
  • I think you have to pass the whole array by reference not just key.. Commented Nov 15, 2012 at 5:19

3 Answers 3

8
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
)
Sign up to request clarification or add additional context in comments.

Comments

1

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));
}

1 Comment

Because I have no control over the building of the array. That snippet I posted was another users code to use as reference
0

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.