chiprivers Posted February 20, 2010 Share Posted February 20, 2010 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. Link to comment https://forums.phpfreaks.com/topic/192710-using-the-value-of-a-variable-to-name-an-array/ Share on other sites More sharing options...
khr2003 Posted February 20, 2010 Share Posted February 20, 2010 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)); Link to comment https://forums.phpfreaks.com/topic/192710-using-the-value-of-a-variable-to-name-an-array/#findComment-1015174 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.