Page 1 of 1

Appending MySQL results to a multidimensional array

Posted: Thu Jul 31, 2008 5:37 pm
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

Re: Appending MySQL results to a multidimensional array

Posted: Thu Jul 31, 2008 6:28 pm
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

Re: Appending MySQL results to a multidimensional array

Posted: Thu Jul 31, 2008 6:47 pm
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]).

Re: Appending MySQL results to a multidimensional array

Posted: Fri Aug 01, 2008 4:48 am
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!