如何删除数组的键名中的空格在PHP? [英] How to remove spaces in array keys names in php?

查看:580
本文介绍了如何删除数组的键名中的空格在PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除数组的键名的所有空间,即str_replace函数('','',$值)

I am trying to remove all spaces in array keys names i.e. str_replace(' ','',$value) (or worst cast scenario replace them with underscores (_) )

和我想在我多维数组的最深层次(如下图所示)(因为其他层/水平没有空格(感谢上帝!))

and I am trying to do this at the deepest level (shown below) of my multidimensional array (because other layers/levels don't have spaces (THANK GOD!))

[...]

[ownPagestoriesbystorytype] => Array
                        (
                            [type] => pagestoriesbystorytype
                            [object_id] => 12365478954
                            [metric] => page_stories_by_story_type
                            [end_time] => 1386057600
                            [period] => 86400
                            [ownValues] => Array
                                (
                                    [type] => pagestoriesbystorytypemetrics
                                    [fan] => 1913
                                    [page post] => 153
                                    [user post] => 24
                                )

                        )

                    [ownPagestorytellersbystorytype] => Array
                        (
                            [type] => pagestorytellersbystorytype
                            [object_id] => 12365478954
                            [metric] => page_storytellers_by_story_type
                            [end_time] => 1386057600
                            [period] => 86400
                            [ownValues] => Array
                                (
                                    [type] => pagestorytellersbystorytypemetrics
                                    [fan] => 1902
                                    [page post] => 137
                                    [user post] => 9
                                )

                        )

[...]

到目前为止,我尝试都石沉大海:

So far my attempts have been fruitless :

[...]
if (is_array($value))
        {

            $keys = str_replace(' ','',array_keys($value));
            $values = array_values($value);
            $value = array_combine($keys,$values);
        }
[...]


[...]

foreach ($value as $k => $v)
            {
                $b = str_replace(' ','',$k);
                $value[$b] = $value[$k];
                unset ($value[$k]);

            }

[...]

在codeS以上不工作,但是如果我把的print_r($值);在循环结束时,你可以清楚地看到,空间被删除,不知怎么竟最终的结果结束了空间(STILL)。

The codes above do not work, however if I put print_r($value); at the end of the loop you can clearly see that spaces are being removed, just somehow the end result ends up being with spaces (STILL).

整个循环看起来是这样的:

The whole loop looks like this:

for ($i=0;$i<count($results);$i++)
{

    for ($j=0;$j<count($results[$i]);$j++)
    {
    foreach($results[$i][$j] as $key => $value)
    {
        $typee = ['type' => strtolower(str_replace('_','',$results[$i][$j]['metric']))];
        array_insert($results[$i][$j],$typee,0);
        if (is_array($value))
        {

            $keys = str_replace(' ','',array_keys($value));
            $values = array_values($value);
            $value = array_combine($keys,$values);

            $type = ['type' => strtolower(str_replace('_','',$results[$i][$j]['metric']))."metrics"];
            array_insert($results[$i][$j]['value'],$type,0);
            $results[$i][$j]['ownValues'] = $results[$i][$j][$key];
            unset($results[$i][$j][$key]);


        }
    }
    }
}

你可以看到整个阵列的样子在这里:

And you can see how the whole array looks like here:

<一个href=\"http://stackoverflow.com/questions/20548052/how-to-$p$ppend-array-to-each-element-of-another-array-with-my-choice-of-key-and\">How以prePEND阵列与我选择的另一个数组中的每个元素键和值(在PHP)?

有什么建议? :)

推荐答案

这将帮助:

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

如下测试:

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

输入:

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
)

这篇关于如何删除数组的键名中的空格在PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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