Appending MySQL results to a multidimensional array
Posted: Thu Jul 31, 2008 5:37 pm
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:
Sorry if this question is really basic, google failed me on this one.
Will
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);Will