Appending MySQL results to a multidimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
deafpanda
Forum Newbie
Posts: 7
Joined: Thu Jul 31, 2008 5:25 pm

Appending MySQL results to a multidimensional array

Post by deafpanda »

Hi everyone,

I'm quite new to PHP, and my multitude of books and google searches haven't found me an answer to this problem:

I have a one-dimensional array, and would like to add the results of a mysql query on to it.

To give you a better idea what I'm talking about:

$fields = array("", "jan", "feb", "mar" etc...)

I want this array to become

$fields = array(array("", "jan", "feb", "mar" etc...),
array(row1result1, row1result2, row1result3, row1result4 etc...),
array(row2result1, row2result2 etc...)

My code that totally didn't work was:

Code: Select all

 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){
    $data[] = array($row['id'], $row['jan'], $row['feb'], $row['mar'], $row['apr'], $row['may'], $row['jun'], $row['jul'], $row['aug'], $row['sep'], $row['oct'], $row['nov'], $row['dcb']);
}
$tabledata = array_merge($fields, $data);
Sorry if this question is really basic, google failed me on this one.

Will
ody3307
Forum Newbie
Posts: 21
Joined: Wed Jul 30, 2008 7:29 am

Re: Appending MySQL results to a multidimensional array

Post by ody3307 »

No, it's not simple at all. Multi-diminsional arrays can be quite difficult to visualize.
It looks like you are trying to produce a date-related array organized by an "id" number and a month. Is this correct? If so, i would approach it like this:

Use the row id as the first level of your new array
field['id]

Then add the months to each row id array
field['id']['jan']
field['id']['feb']
field['id']['mar']
...

After your database query, you might have something like this:
field['302']['jan']='cat'
field['302']['feb']='dog'
field['302']['mar']='parrot'
...
field['468']['jan']='bmw'
field['468']['feb']='audi'
field['468']['mar']='ford'

Now you have an array of values you can access in a variety of ways.
$new_array = field['302'] would produce an array of jan thru dec values for row 302
$new_item = field['302']['mar'] would produce the value of mar in row 302

You get your values into your new array like this:
while(code for sql query here) {
$i = $row['id'];
field[$i]['jan'] = $row['jan'];
field[$i]['feb'] = $row['feb'];
field[$i]['mar'] = $row['mar'];
...
}

Make sense? Of course, if I didn't understand what you are trying to do, this won't solve the problem! LOL

Let me know...
Mark
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Appending MySQL results to a multidimensional array

Post by Christopher »

It would be a lot easier if you use just used the rows from the DB directly:

Code: Select all

$tabledata = array();
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){
    $tabledata[] = $row;
}
// now dump it
echo '<pre>' . print_r($tabledata, true) . '</pre>';
if you want to export it with a header row then just get array_keys($tabledata[0]).
(#10850)
deafpanda
Forum Newbie
Posts: 7
Joined: Thu Jul 31, 2008 5:25 pm

Re: Appending MySQL results to a multidimensional array

Post by deafpanda »

Thanks for your time guys, very helpful.

I'd never heard of the array_keys function before, but it's exactly what I was looking for.

Cheers!
Post Reply