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
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'];
   }

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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/>'; 
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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