Page 1 of 1

Very simple array question

Posted: Tue Aug 03, 2010 12:15 pm
by rhecker
I want to build an array from two values in a MySQL table. Although the code sample below doesn't work, I think it will show what I'm trying to do:

Code: Select all

$sql=mysql_query("SELECT id, label FROM table");
while ($form=mysql_fetch_array($sql)){
$labelset= array($form[id] => $form[label]);
}
echo $labelset[14];	
echo $labelset[5];
I appreciate any help.

Re: Very simple array question

Posted: Tue Aug 03, 2010 12:31 pm
by AbraCadaver
Two ways depending on what you want:

Code: Select all

// multidimensional array $labelset[0][1] = 'some label' / assume the 1 is a form id
$sql=mysql_query("SELECT id, label FROM table");
while ($form=mysql_fetch_array($sql)){
   $labelset[] = array($form['id'] => $form['label']);
}
//or 

// array $labelset[1] = 'some label' / assume the 1 is a form id
$sql=mysql_query("SELECT id, label FROM table");
while ($form=mysql_fetch_array($sql)){
   $labelset[$form['id']] = $form['label'];
}

Re: Very simple array question

Posted: Tue Aug 03, 2010 1:44 pm
by rhecker
Thank you for both samples. The second one was exactly what I needed.