ginga8 Posted January 23, 2009 Share Posted January 23, 2009 I have the following multidimensional array, I am pretty new to this. $sorted = array(); foreach($events as $event) { $year = date('Y', strtotime($event->start_date)); if(empty($sorted[$year]['total_hours'])) { $sorted[$year]['total_hours'] = 0; } $sorted[$year]['event'][] = $event->title; $sorted[$year]['event_start_date'][] = $event->start_date; $sorted[$year]['event_hours'][] = $event->workshop_hours_4; $sorted[$year]['total_hours'] += $event->workshop_hours_4; } With the year being dynamic, how would I output it to look something like the following 2007 EventID1 - 4 hours EventID2 - 5 hours TOTAL HOURS - 9 hours 2008 EventID3 - 5 hours EventID4 - 2 hours EventID5 - 3 hours TOTAL HOURS - 10 hours and so on. Any suggestions would be great Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142103-help-outputting-multidimensional-array/ Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 Where does this data come from? A database? You should sort that by Year DESC it is better to sort data where it is easily sorted. Doing that you can create this array just like you have it with no problems. Quote Link to comment https://forums.phpfreaks.com/topic/142103-help-outputting-multidimensional-array/#findComment-744242 Share on other sites More sharing options...
rhodesa Posted January 23, 2009 Share Posted January 23, 2009 i would do it a little differently. the title, hours, etc are already in a nice object. so just make an array of those objects. also, we can calculate the total later when we print it: <?php $sorted = array(); foreach($events as $event) { $year = date('Y', strtotime($event->start_date)); if(!isset($sorted[$year])) $sorted[$year] = array(); $sorted[$year][] = $event; } foreach($sorted as $year => $s_events){ $total = 0; print "<b>$year</b><br />\n"; foreach($s_events as $s_event){ print "{$s_event->title} - {$s_event->workshop_hours_4} hours<br />\n"; $total += $s_event->workshop_hours_4; } print "<b>TOTAL HOURS - {$total} hours</b><br /><br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142103-help-outputting-multidimensional-array/#findComment-744243 Share on other sites More sharing options...
ginga8 Posted January 23, 2009 Author Share Posted January 23, 2009 Here is my function getting the info function civicrm_workshop_hours_SB_get_event() { $events = array(); $sql = db_query('SELECT civicrm_contact.first_name, civicrm_contact.last_name, civicrm_event.title, civicrm_event.start_date, civicrm_event.end_date, civicrm_uf_match.uf_name, civicrm_uf_match.uf_id, civicrm_participant.status_id, civicrm_value_total_hours_2.workshop_hours_4 FROM {swiftrad_civicrm.civicrm_contact} Inner Join {swiftrad_civicrm.civicrm_participant} ON civicrm_contact.id = civicrm_participant.contact_id Inner Join {swiftrad_civicrm.civicrm_event} ON civicrm_participant.event_id = civicrm_event.id Inner Join {swiftrad_civicrm.civicrm_uf_match} ON civicrm_uf_match.contact_id = civicrm_contact.id Inner Join {swiftrad_civicrm.civicrm_value_total_hours_2} ON civicrm_value_total_hours_2.entity_id = civicrm_event.id WHERE civicrm_participant.status_id = 2 '); // put into array while ($event = db_fetch_object($sql)) { $events[] = $event; } return $events; } Quote Link to comment https://forums.phpfreaks.com/topic/142103-help-outputting-multidimensional-array/#findComment-744257 Share on other sites More sharing options...
rhodesa Posted January 23, 2009 Share Posted January 23, 2009 as premiso said, if you sort the events DESC by date, you can do it without an extra sort...it's a little more complicated though: <?php $last_year = null; $total = 0; if(count($events)){ foreach($events as $event) { $year = date('Y', strtotime($event->start_date)); if($year != $last_year){ if(!empty($last_year)) print "<b>TOTAL HOURS - {$total} hours</b><br /><br />\n"; print "<b>$year</b><br />\n"; $last_year = $year; $total = 0; } print "{$event->title} - {$event->workshop_hours_4} hours<br />\n"; $total += $event->workshop_hours_4; } print "<b>TOTAL HOURS - {$total} hours</b><br /><br />\n"; } ?> ...the above should work, but is untested Quote Link to comment https://forums.phpfreaks.com/topic/142103-help-outputting-multidimensional-array/#findComment-744267 Share on other sites More sharing options...
ginga8 Posted January 23, 2009 Author Share Posted January 23, 2009 Thanks so much, I tried to get this working for hours Quote Link to comment https://forums.phpfreaks.com/topic/142103-help-outputting-multidimensional-array/#findComment-744289 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.