I have the following array (via var_dump) $results:
array(4) {
[0]=> array(5) {
[0]=> array(1) { ["evtId"]=> string(1) "2" }
[1]=> array(1) { ["evtId"]=> string(1) "3" }
[2]=> array(1) { ["evtId"]=> string(1) "4" }
[3]=> array(1) { ["evtId"]=> string(1) "5" }
[4]=> array(1) { ["evtId"]=> string(1) "6" }
[1]=> array(5) {
[0]=> array(1) { ["evtLocation"]=> string(2) "11 St Paul" }
[1]=> array(1) { ["evtLocation"]=> string(5) "12412 Horace St" }
[2]=> array(1) { ["evtLocation"]=> string(14) "Friends Center" }
[3]=> array(1) { ["evtLocation"]=> string(14) "Friends Center" }
[4]=> array(1) { ["evtLocation"]=> string(14) "Friends Center" }
[2]=> array(5) {
[0]=> array(1) { ["evtDate"]=> string(1) "11/12/2011" }
[1]=> array(1) { ["evtDate"]=> string(1) "06/05/2012" }
[2]=> array(1) { ["evtDate"]=> string(1) "10/10/2010" }
[3]=> array(1) { ["evtDate"]=> string(1) "06/06/2012" }
[4]=> array(1) { ["evtDate"]=> string(1) "10/12/2012" }
[3]=> array(5) {
[0]=> array(1) { ["evtType"]=> string(4) "Fun" }
[1]=> array(1) { ["evtType"]=> string(6) "Random" }
[2]=> array(1) { ["evtType"]=> string(9) "Childcare" }
[3]=> array(1) { ["evtType"]=> string(9) "Childcare" }
[4]=> array(1) { ["evtType"]=> string(9) "Childcare" }
I’m using this array to pull database information into a function that builds a table. However, I need this to end up in the following format:
$rows[] = array('2', '11 St Paul', '11/12/2011', 'Fun');
I’ve really only been able to get this far:
foreach ($events as $field_arr) {
//this gives me 4 arrays, each array containing all of the records for one field type.
}
Now I need to loop through each of the four arrays, take one value from the same index and add it to the $rows[] array, which I can pass to the table building function. I’ve tried variations of the following (inside the initial foreach loop)
$x = count($result[0]); //this gives the number of fields
for ($i = 0; $i < $x; $i++) {
rows[$i] = $field_array[$i];
}
I’ve been trying variations for half the day, with little luck (I’m ending up with arrays containing 4 times as many elements as I need, and having a hard time getting rid of the final array keys (['evtType'] etc).
If anyone can help point me in the right direction I’d greatly appreciate it.
Most code posted here seems quite verbose, so here’s a short version:
That’s pretty much it. Each element of
$resultsholds a list of key=>value pairs and we’re only interested in the value of each pair. The index of the pair is the index of the row, which we store in$rowNumber. I’ve run it and it seems to produce the expected results.