member123 Posted May 13, 2008 Share Posted May 13, 2008 I am converting rows in a MySQL table into rows in an array. The table looks like this: player_id player age 1Joe24 2Craig31 3David29 ... with a bunch of rows like that. So far my code looks like this: $result = bb_db_query("SELECT * FROM bb_players"); while ($myrow=mysql_fetch_array($result)) { $players[$myrow['player_id']][$myrow['player']][] = $myrow['age']; } Am I doing it correctly so far? Also, could someone please help me create code that will print out all of this information using foreach() commands? Thanks! Link to comment https://forums.phpfreaks.com/topic/105387-storing-two-data-pieces-in-each-array-entry/ Share on other sites More sharing options...
jaymc Posted May 13, 2008 Share Posted May 13, 2008 Try this $result = bb_db_query("SELECT * FROM bb_players"); while ($myrow=mysql_fetch_assoc($result)) { // $each_row will now contain this - 1,Joe,24 // to turn it back into an array, do this - $each_row = explode(",", $each_row); $each_row = implode(",", $myrow); } The foreach part, you can do this foreach ($each_row as $each_row) { // this will give - 1<BR>Joe<BR>24 echo "$each_row<BR>"; } Link to comment https://forums.phpfreaks.com/topic/105387-storing-two-data-pieces-in-each-array-entry/#findComment-539747 Share on other sites More sharing options...
member123 Posted May 13, 2008 Author Share Posted May 13, 2008 Hi Jaymc, that first part looks like it's exactly what I'm looking for. Looks like you found an interesting way to do that part. However, for the second part, I'm going to be doing some analysis with the names and ages. How do I separate that information? Thanks. Link to comment https://forums.phpfreaks.com/topic/105387-storing-two-data-pieces-in-each-array-entry/#findComment-539757 Share on other sites More sharing options...
trq Posted May 13, 2008 Share Posted May 13, 2008 How do I separate that information? Why are you putting it togther in the first place? Link to comment https://forums.phpfreaks.com/topic/105387-storing-two-data-pieces-in-each-array-entry/#findComment-539758 Share on other sites More sharing options...
member123 Posted May 13, 2008 Author Share Posted May 13, 2008 It's the nature of the beast. Link to comment https://forums.phpfreaks.com/topic/105387-storing-two-data-pieces-in-each-array-entry/#findComment-539759 Share on other sites More sharing options...
jaymc Posted May 13, 2008 Share Posted May 13, 2008 Hi Jaymc, that first part looks like it's exactly what I'm looking for. Looks like you found an interesting way to do that part. However, for the second part, I'm going to be doing some analysis with the names and ages. How do I separate that information? Thanks. foreach ($each_row as $each_row) { // this will give - 1<BR>Joe<BR>24 echo "$each_row<BR>"; } That will spit out all entries in the array Or $each_row = explode(",", $each_row); $each_row[0] will give you the ID $each_row[1] will give you the name $each_row[2] will give you the age Logical order. Think thats what you need? If not please explain Link to comment https://forums.phpfreaks.com/topic/105387-storing-two-data-pieces-in-each-array-entry/#findComment-539785 Share on other sites More sharing options...
member123 Posted May 14, 2008 Author Share Posted May 14, 2008 Is there a way to do this without implode/explode? I am getting this error: "Invalid argument supplied for foreach()" and I'm also worried about using commas to explode because some of the names are going to contain commas. Link to comment https://forums.phpfreaks.com/topic/105387-storing-two-data-pieces-in-each-array-entry/#findComment-540772 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.