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? Quote Link to comment 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(); ? Quote Link to comment 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 Quote Link to comment 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); Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.