I have some code which pulls from multiple tables of a MS SQL database, arranges the data and outputs an array, I then need to use that array to populate a worksheet telling someone what items are needed on a day to day basis. Right now the output looks like the below, where the fields are ID, Name, Date, number (they are asterisk deliminated because some names include commas).
74*xxxx*2017-06-04*1
74*xxxx*2017-06-05*1
74*xxxx*2017-06-06*2
74*xxxx*2017-06-07*2
74*xxxx*2017-06-08*2
74*xxxx*2017-06-09*2
74*xxxx*2017-06-10*2
75*yyyy*2017-06-05*123
75*yyyy*2017-06-06*111
75*yyyy*2017-06-07*124
75*yyyy*2017-06-08*107
75*yyyy*2017-06-09*194
75*yyyy*2017-06-10*195
I am trying to get them to merge them into a single value per ID so that I can print it out to a grid to look similar to:
| ID | NAME | 06-04 | 06-05 | 06-06 | 06-07 | 06-08 | 06-09 | 06-10 |
| 74 | xxxx | 1 | 1 | 2 | 2 | 2 | 2 | 2 |
| 75 | yyyy | - | 123 | 111 | 124 | 107 | 194 | 195 |
my intent was to merge the array values into a single array with the dates as the keys, the (most recent) code I was trying is:
$schedule[] = $PID . "*" . $program . "*" . $date . "*" . $num;
$grid = array();
foreach ($schedule as $item ) {
$ex = explode('*', $item);
$grid[$ex[0]] = array(
'name' => $ex[1],
$ex[2] => $ex[3],
);
}
with the intent that that would merge them into a single array line to look like:
[74] => Array ( [name] => xxxx [2017-06-04] => 1 [2017-06-05] => 1 [2017-06-06] => 2 [2017-06-07] => 2 [2017-06-08] => 2 [2017-06-09] => 2 [2017-06-10] => 2)
after which I could just use it line by line with $grid[$i][1] etc to pull the value and print to each cell. I've tried other methods than the above, that is the only one I presently have the code snippet for and I've only been able to get it to keep the last value I add on a per ID basis.
I'm certain there is a piece I am missing or simply not understanding, if anyone could offer some input it would be appreciated.