Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing array key spaces with underscores in multidimensional array

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;
    }
}
like image 557
Bankzilla Avatar asked Jun 24 '26 03:06

Bankzilla


1 Answers

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
)
like image 190
Passerby Avatar answered Jun 26 '26 18:06

Passerby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!