Jump to content

Remove spaces from array keys


clay1

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.