Jump to content

Using the value of a variable to name an array?


chiprivers

Recommended Posts

I would like to be able to create an array using the value of a variable extracted from my database.

 

I have extracted some values from my database:

$result = mysql_query($query);

and I want to loop through the results creating a multi-dimensional array using one of the values from the database as a key within the array:

while($row = mysql_fetch_array($result))
{
$multi_array => array($row[key] => array(a => $row[a], b => $row[b].....))
}

 

Where I have used $row[key] within the second level array I want the value stored in $row[key] to be used to name the array.

 

Any offers? I am sure I have done this before but can not remember how and can not find any reference material.

you can do it this way:

while($row = mysql_fetch_array($result))
{
// extract row values for an easier use of vars
extract($row);
// this will generate a multi-dimensional array
// look at the format of the array, you can modify it as you need
$array[$field1][$field2] = $field3;
}
// print the result, just checking
echo nl2br(print_r($array,1));

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.