Jump to content

Multidimensional Array from MySQL Table


member123

Recommended Posts

I currently have a MySQL table that is set up like this:

team_id team player
1 Expos David
1 Expos James
1 Expos Jeremy
2 Pirates Alex
2 Pirates Steve

 

To pull this table into an array, I have this code:

  $result = bb_db_query("SELECT * FROM bb_players");

  if ($myrow=mysql_fetch_array($result)) {

      do {

        $players[] = $myrow["player"];

    }while ($myrow=mysql_fetch_array($result));

  }

 

where bb_db_query is basically a function that queries the database and bb_players is the name of the table.  However, I want to turn the array into a multidimensional array that makes use of the team IDs and teams as well.

 

How should I go about modifying my code to make a multidimensional array that includes the team IDs and team names linked together, and has sub-arrays consisting of the player names for each teams?  I think I can figure out everything except how to include both the IDs and the team names.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/105214-multidimensional-array-from-mysql-table/
Share on other sites

   $result = bb_db_query("SELECT * FROM bb_players");
   while ($myrow=mysql_fetch_array($result)) {
      $team[myrow['team_id']]['name'] = $myrow['team'];
      $team[myrow['team_id']]['players'][] = $myrow['player'];
   }

 

I'm getting this error:

 

Parse error: syntax error, unexpected '[', expecting ']' in /home/ticker/public_html/index.php on line 12

 

Line 12 is

 

      $team[myrow['team_id']]['name'] = $myrow['team'];

 

I declare the array with this:

 

$team = array();

 

How should I adjust that to declare it as a multidimensional array?

With the one-dimension array, I had a foreach statement to display the contents of the array.

 

With the multidimensional array, I tried using two foreach statements, but it doesn't seem to be working correctly.  How should I set things up differently now?

 

Thanks.

Something like

<?php
$teams = array (
		1 => array (
		    	'name' => 'Expos',
		    	'players' => array ('David','James','Jeremy')
			) ,
		2 => array (
		    	'name' => 'Pirates',
		    	'players' => array ('Alex','Steve')
			)
);

foreach ($teams as $id => $teamdata)
{
echo "$id : " . $teamdata['name'] . '<br/>';
foreach ($teamdata['players'] as $player)
{
	echo $player . '<br/>';
}
echo '<br/>'; 
}
?>

Thanks Barand, that code works great when the array is hard coded like that.

 

However, with this code:

   $result = bb_db_query("SELECT * FROM bb_players");
   while ($myrow=mysql_fetch_array($result)) {
      $team[$myrow['team_id']]['name'] = $myrow['team'];
      $team[$myrow['team_id']]['players'][] = $myrow['player'];
   }

 

    used to fill the array, it doesn't seem to be working correctly.  Do you see something that I should change with this code?

 

Thanks.

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.