By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

MySQL table to PHP multidimensional array

P: n/a
Let's say I have this MySQL table.

Year Region Amount
2007 West 100
2007 East 200
2007 North 300
2007 South 400
2008 West 500
2008 East 600
2008 North 700
2008 South 800

How would I get this table into a multidimensional array in PHP. I'm
looking for code that will generate the array below automatically from
a MySQL table/query. Thanks...

tbl[] =
array("2007" =array("West" =100, "East" =200, "North" =300,
"South" =400),
"2008" =array("West" =500, "East" =600, "North" =700,
"South" =800))

Sep 3 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
mo*****@yahoo.com wrote:
How would I get this table into a multidimensional array in PHP.
Something like this:

$multiarray = array();
while ($result = mysql_fetch_assoc())
{
$year = $result['year'];
$multiarray[$year] = $result;
}
Sep 3 '08 #2

P: n/a
Thanks, Sjoerd. I'll give it shot. Looks like it should work
though. However, shouldn't it be like this?

$multiarray = array();
while ($result = mysql_fetch_assoc())
{
$year = $result['year'];
$region = $result['region'];
$amount= $result['amount'];
$multiarray[$year][$region][] = $amount;
}
Sep 4 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.