title: How to remove spaces in array keys names in php?
body: <p>I am trying to remove all spaces in array keys names i.e. str_replace(' ','',$value) (or worst cast scenario replace them with underscores (_) ) </p>
  
  <p>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!))</p>
  
  <pre><code>[...]
  
  [ownPagestoriesbystorytype] =&gt; Array
                          (
                              [type] =&gt; pagestoriesbystorytype
                              [object_id] =&gt; 12365478954
                              [metric] =&gt; page_stories_by_story_type
                              [end_time] =&gt; 1386057600
                              [period] =&gt; 86400
                              [ownValues] =&gt; Array
                                  (
                                      [type] =&gt; pagestoriesbystorytypemetrics
                                      [fan] =&gt; 1913
                                      [page post] =&gt; 153
                                      [user post] =&gt; 24
                                  )
  
                          )
  
                      [ownPagestorytellersbystorytype] =&gt; Array
                          (
                              [type] =&gt; pagestorytellersbystorytype
                              [object_id] =&gt; 12365478954
                              [metric] =&gt; page_storytellers_by_story_type
                              [end_time] =&gt; 1386057600
                              [period] =&gt; 86400
                              [ownValues] =&gt; Array
                                  (
                                      [type] =&gt; pagestorytellersbystorytypemetrics
                                      [fan] =&gt; 1902
                                      [page post] =&gt; 137
                                      [user post] =&gt; 9
                                  )
  
                          )
  
  [...]
  </code></pre>
  
  <p>So far my attempts have been fruitless :</p>
  
  <pre><code>[...]
  if (is_array($value))
          {
  
              $keys = str_replace(' ','',array_keys($value));
              $values = array_values($value);
              $value = array_combine($keys,$values);
          }
  [...]
  
  
  [...]
  
  foreach ($value as $k =&gt; $v)
              {
                  $b = str_replace(' ','',$k);
                  $value[$b] = $value[$k];
                  unset ($value[$k]);
  
              }
  
  [...]
  </code></pre>
  
  <p>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).</p>
  
  <p>The whole loop looks like this:</p>
  
  <pre><code>for ($i=0;$i&lt;count($results);$i++)
  {
  
      for ($j=0;$j&lt;count($results[$i]);$j++)
      {
      foreach($results[$i][$j] as $key =&gt; $value)
      {
          $typee = ['type' =&gt; 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' =&gt; 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]);
  
  
          }
      }
      }
  }
  </code></pre>
  
  <p>And you can see how the whole array looks like here: </p>
  
  <p><a href="https://stackoverflow.com/questions/20548052/how-to-prepend-array-to-each-element-of-another-array-with-my-choice-of-key-and">How to prepend array to each element of another array with my choice of key and value (in php)?</a></p>
  
  <p>Any suggestions? :)</p>