clay1 Posted March 4, 2010 Share Posted March 4, 2010 I've got an associative array from pg_fetch_array which ends up having spaces in some of the keys due to spaces in the column names(no I can't change them). I want to use extract($array) but obviously the spaces create an issue accessing my variables. How can I go from $lead = pg_fetch_array($result, $i, PGSQL_ASSOC); extract($lead); To something where I won't have any spaces? Link to comment https://forums.phpfreaks.com/topic/194104-remove-spaces-from-array-keys/ Share on other sites More sharing options...
Rustywolf Posted March 4, 2010 Share Posted March 4, 2010 Your message wasnt very clear ... maybe your looking for explode(); ? Link to comment https://forums.phpfreaks.com/topic/194104-remove-spaces-from-array-keys/#findComment-1021292 Share on other sites More sharing options...
clay1 Posted March 4, 2010 Author Share Posted March 4, 2010 My pg_fetch_array returns something like: $lead['id'] $lead['street address'] I need that to get rid of that space Link to comment https://forums.phpfreaks.com/topic/194104-remove-spaces-from-array-keys/#findComment-1021296 Share on other sites More sharing options...
Rustywolf Posted March 4, 2010 Share Posted March 4, 2010 either str_replace(" ", "_", $input); or str_replace(" ", "", $input); Link to comment https://forums.phpfreaks.com/topic/194104-remove-spaces-from-array-keys/#findComment-1021303 Share on other sites More sharing options...
clay1 Posted March 4, 2010 Author Share Posted March 4, 2010 Yeah but how do I do that to the keys? I tried that it just removes the space from the value not the key Link to comment https://forums.phpfreaks.com/topic/194104-remove-spaces-from-array-keys/#findComment-1021305 Share on other sites More sharing options...
Rustywolf Posted March 4, 2010 Share Posted March 4, 2010 Oh. Maybe, try setting it as a variable $street_address = $lead['street address'] or change the value in config/mysql/whatever Link to comment https://forums.phpfreaks.com/topic/194104-remove-spaces-from-array-keys/#findComment-1021310 Share on other sites More sharing options...
clay1 Posted March 4, 2010 Author Share Posted March 4, 2010 Oh. Maybe, try setting it as a variable $street_address = $lead['street address'] or change the value in config/mysql/whatever Well that is what the extract() is for. I was trying to avoid typing out each of the hundred variables. Like I said I can't change the column names. Isn't there a way I can create a new array with the correct key names? like foreach($lead) $lead['key'] = str_replace($lead['key'] Something along those lines Link to comment https://forums.phpfreaks.com/topic/194104-remove-spaces-from-array-keys/#findComment-1021311 Share on other sites More sharing options...
clay1 Posted March 4, 2010 Author Share Posted March 4, 2010 foreach($lead as $key => $value){ echo "<br><br>$key"; $key= str_replace( ' ', '', $key ); echo "<br><br>$key"; } The second $key is correct but it doesn't stick. In other words when I do that then dump $lead I still have the old keys Link to comment https://forums.phpfreaks.com/topic/194104-remove-spaces-from-array-keys/#findComment-1021316 Share on other sites More sharing options...
clay1 Posted March 4, 2010 Author Share Posted March 4, 2010 Solved it. $lead = pg_fetch_array($result, $i, PGSQL_ASSOC); //Remove spaces from column names $keys = str_replace( ' ', '', array_keys($lead) ); $values = array_values($lead); $lead = array_combine($keys, $values); Link to comment https://forums.phpfreaks.com/topic/194104-remove-spaces-from-array-keys/#findComment-1021318 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.