<bdo id='CwhPd'></bdo><ul id='CwhPd'></ul>

    1. <tfoot id='CwhPd'></tfoot>

        <small id='CwhPd'></small><noframes id='CwhPd'>

        <legend id='CwhPd'><style id='CwhPd'><dir id='CwhPd'><q id='CwhPd'></q></dir></style></legend>
        <i id='CwhPd'><tr id='CwhPd'><dt id='CwhPd'><q id='CwhPd'><span id='CwhPd'><b id='CwhPd'><form id='CwhPd'><ins id='CwhPd'></ins><ul id='CwhPd'></ul><sub id='CwhPd'></sub></form><legend id='CwhPd'></legend><bdo id='CwhPd'><pre id='CwhPd'><center id='CwhPd'></center></pre></bdo></b><th id='CwhPd'></th></span></q></dt></tr></i><div id='CwhPd'><tfoot id='CwhPd'></tfoot><dl id='CwhPd'><fieldset id='CwhPd'></fieldset></dl></div>

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

        时间:2023-09-21
        <i id='n3aaK'><tr id='n3aaK'><dt id='n3aaK'><q id='n3aaK'><span id='n3aaK'><b id='n3aaK'><form id='n3aaK'><ins id='n3aaK'></ins><ul id='n3aaK'></ul><sub id='n3aaK'></sub></form><legend id='n3aaK'></legend><bdo id='n3aaK'><pre id='n3aaK'><center id='n3aaK'></center></pre></bdo></b><th id='n3aaK'></th></span></q></dt></tr></i><div id='n3aaK'><tfoot id='n3aaK'></tfoot><dl id='n3aaK'><fieldset id='n3aaK'></fieldset></dl></div>

              <legend id='n3aaK'><style id='n3aaK'><dir id='n3aaK'><q id='n3aaK'></q></dir></style></legend>
                <tbody id='n3aaK'></tbody>

              <small id='n3aaK'></small><noframes id='n3aaK'>

              • <bdo id='n3aaK'></bdo><ul id='n3aaK'></ul>
                <tfoot id='n3aaK'></tfoot>
                  本文介绍了如何在php中删除数组键名称中的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试删除数组键名称中的所有空格,即 str_replace(' ','',$value) (或者最坏的转换场景将它们替换为下划线 (_) )

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

                  上面的代码不起作用,但是如果我把 print_r($value);在循环结束时,您可以清楚地看到空格正在被删除,只是不知何故最终结果以空格结束(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).

                  整个循环如下所示:

                  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:

                  如何使用我选择的键和值(在 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中删除数组键名称中的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用“optgroup"选项选择下拉 PHP foreach 循环 下一篇:php - foreach - 通过键名获取具体数据

                  相关文章

                  最新文章

                  <i id='nhUW2'><tr id='nhUW2'><dt id='nhUW2'><q id='nhUW2'><span id='nhUW2'><b id='nhUW2'><form id='nhUW2'><ins id='nhUW2'></ins><ul id='nhUW2'></ul><sub id='nhUW2'></sub></form><legend id='nhUW2'></legend><bdo id='nhUW2'><pre id='nhUW2'><center id='nhUW2'></center></pre></bdo></b><th id='nhUW2'></th></span></q></dt></tr></i><div id='nhUW2'><tfoot id='nhUW2'></tfoot><dl id='nhUW2'><fieldset id='nhUW2'></fieldset></dl></div>
                  1. <tfoot id='nhUW2'></tfoot>
                    • <bdo id='nhUW2'></bdo><ul id='nhUW2'></ul>
                  2. <small id='nhUW2'></small><noframes id='nhUW2'>

                      <legend id='nhUW2'><style id='nhUW2'><dir id='nhUW2'><q id='nhUW2'></q></dir></style></legend>