在多维数组下划线替代数组的键位 [英] Replacing array key spaces with underscores in multidimensional array

查看:137
本文介绍了在多维数组下划线替代数组的键位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有所有的整个键位的数组。这使得问题在其他程序针对无法目标空间,是不好的做法,有空格键。

我在找的东西,将多维数组中拔出钥匙空间和替换用下划线。最有可能将必须是一个递归函数?

在发现另一个问题,类似的东西,但是是关于值替换。

 的foreach($ all_regions为$关键=> $值){
 $ all_regions [$关键] =用strtolower(str_replace函数('','_',$值));
}

pretty多么需要这个复制的,但钥匙。
我打的问题是,我能想到的如何参考密钥本身,因为如果你试图推喜欢上面的方法它只是重新用下划线的另一个关键。

数组的一个片段,这个,因为它走的是深。

 阵列

    [0] =>排列
        (
            [行标识符] => PID
            [设置ID] => 1
            [用户ID] =>
            [要求] =>排列
                (
                    [0] =>排列
                        (
                            [行标识符] => OBR
                            [设置ID] => 1
                            [砂矿订单号] => 021120091525
                            [结果] =>排列
                                (
                                    [0] =>排列
                                        (
                                            [行标识符] => OBX
                                            [设置ID] => 1
                                    [1] =>排列
                                        (
                                            [行标识符] => OBX
                                            [设置ID] => 2

我试过以下,但关键元素不能做个参考

 私有函数fixArrayKeys($数组){
    如果(is_array($阵列)){
        的foreach($数组和放大器; $关键=> $值){
            如果(!is_array($键))
                $阵列[用strtolower(str_replace函数('','_',$键))] = $价值;
            其他
                fixArrayKeys($数组);
        }
    }其他{
        返回$阵列;
    }
}


解决方案

函数fixArrayKey(安培; $ ARR)
{
    $ ARR = array_combine(array_map(函数($ STR){返回str_replace函数(,_,$海峡);},array_keys($ ARR)),array_values​​($ ARR));
    的foreach($改编为$关键=> $ VAL)
    {
        如果(is_array($ VAL))fixArrayKey($改编[$关键]);
    }
}

如下测试:

$数据=阵列(键1=>ABC,键2=>阵列( 分1=>ABC,子2=>中DEF),键3=>中GHI);
的print_r($数据);
fixArrayKey($数据);
的print_r($数据);

此输出:

阵列

    [键1] => ABC
    [键2] =>排列
        (
            [1分] => ABC
            [2分] => DEF
        )    [关键3] => GHI

排列

    [key_1] => ABC
    [key_2] =>排列
        (
            [sub_1] => ABC
            [sub_2] => DEF
        )    [KEY_3] => GHI

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

解决方案

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
)

这篇关于在多维数组下划线替代数组的键位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆