chaost Posted February 22, 2008 Share Posted February 22, 2008 Hi, This is bugging me and should be soooooo simple. Help please! I have a myslq table with 3 fields. <id><item><result>. 1 fish 15 4 cats 10 6 frogs 50 I need to build an array like this array( [1] => array([item] => fish, [result] => 15), [4] => array([item] => cats, [result] => 10), [6] => array([item] => frogs,[result] => 50) ) thank you very much in advance for the help!!! Link to comment https://forums.phpfreaks.com/topic/92484-table-to-array-help-so-simple/ Share on other sites More sharing options...
Bauer418 Posted February 22, 2008 Share Posted February 22, 2008 <?php $query = "SELECT * FROM table_name"; $result = mysql_query($query) or die(mysql_error()); $table = array(); while ($row = mysql_fetch_assoc($result)) { $table[$row['id']] = $row; } print_r($table); ?> Link to comment https://forums.phpfreaks.com/topic/92484-table-to-array-help-so-simple/#findComment-473822 Share on other sites More sharing options...
jeremyphphaven Posted February 22, 2008 Share Posted February 22, 2008 totally confused. didn't you just build the array? lol <?php $array = array(); // MYSQL DATABASE LOOP FUNCTIONALITY (assuming you're using MYSQL) while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { array_push($array,$row['id'] => array([item] => $row['item'], [result] => $row['result'])); } ?> Just to note, I didn't test this and just wrote it out here, so may need tweaking but should work just fine. Link to comment https://forums.phpfreaks.com/topic/92484-table-to-array-help-so-simple/#findComment-473823 Share on other sites More sharing options...
chaost Posted February 22, 2008 Author Share Posted February 22, 2008 lol. yes, thanks guys, it was simple. I have just started using an sql class to speed things up and the result array was confusing me. Bringing it back to basic - always helps... Thank you! Link to comment https://forums.phpfreaks.com/topic/92484-table-to-array-help-so-simple/#findComment-473898 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.