value of one column as key for another

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

value of one column as key for another

Post by rhecker »

I have a table 'languages' with columns field, english, french, german.
I have variable $language = 'french'

I want the value of 'field' to become the key for the value of 'french'
For example, if field = city and french = ville, I want to end up with $city having the value ville.

I have tried many ways, including the following, but nothing works. Please put me out of my misery!

Code: Select all

$sql=mysql_query("SELECT field, $language FROM languages");
while($set=mysql_fetch_assoc($sql)){
foreach ($set as $value){
	$now = array($set['field'] => $set[$language]);
}
extract($now);
echo $city;
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: value of one column as key for another

Post by Darhazer »

You have to use variable variables

Code: Select all

$sql=mysql_query("SELECT field, $language FROM languages");
while($set=mysql_fetch_assoc($sql)){
${$set['field']} = $set[$language];
}
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: value of one column as key for another

Post by rhecker »

Thank you! That was it. And thanks for the link to the explanation in the manual, so I understand what's going on.
Post Reply