如何在php中删除数组键名称中的空格


How to remove spaces in array keys names in php?

我正在尝试删除数组键名称中的所有空格,即str_replace('','',$value)(或最坏的强制转换方案用下划线(_)替换它们)

我正试图在我的多维数组的最深处(如下所示)做到这一点(因为其他层/级别没有空间(感谢上帝!))

[...]
[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
                                )
                        )
[...]

到目前为止,我的尝试都没有结果:

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

但是,如果我把print_r($value);在循环的最后,你可以清楚地看到空格被删除了,只是不知怎么的,最终结果是空格(仍然)。

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

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

        }
    }
    }
}

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

如何用我选择的键和值(在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
)

您可以将数组传递给str_replace,这样做会更干净、更容易:

$my_array = array( 'one 1' => '1', 'two 2' => '2' );
$keys = str_replace( ' ', '', array_keys( $my_array ) );
$results = array_combine( $keys, array_values( $my_array ) );

结果:

array(2) {
  ["one1"]=>
  string(1) "1"
  ["two2"]=>
  string(1) "2"
}

示例:https://glot.io/snippets/ejej1chzg3

function array_stripstuff(&$elem)
{
  if (is_array($elem)) {
    foreach ($elem as $key=>$value)
      $elem[str_replace(" ","-",$key)]=$value;
  }
  return $elem;
}
$strippedarray = array_walk_recursive($yourarray,'array_stripstuff');

好了:-)