sctajc Posted September 2, 2009 Share Posted September 2, 2009 I have an array (simplified) loaded as such: Date budget Actual 2008-01-01 120.00 2008-01-01 80.00 2008-02-09 120.00 2008-02-12 90.00 2008-03-01 120.00 2008-03-01 120.00 2008-03-05 10.00 When there is both a 'budget' and 'actual' amount on the same date (eg 2008-01-01 & 2008-03-01), I am wanting to combine to one line (currently there will never be both 'actual' and 'budget' on same date). Had a look through available PHP array functions and felt there must be one that could do this but could not understand them well enough. Tried doing searches but not to sure what to search using. I could try writing code in PHP to combine but a function would do a better job. Quote Link to comment https://forums.phpfreaks.com/topic/172774-combining-array-entries-with-same-date/ Share on other sites More sharing options...
RussellReal Posted September 2, 2009 Share Posted September 2, 2009 so you would like it to.. A) Add up all the budgets from same day.. and B) add up all the actuals from the same day.. than C) make it show the totals in each slot? Quote Link to comment https://forums.phpfreaks.com/topic/172774-combining-array-entries-with-same-date/#findComment-910640 Share on other sites More sharing options...
sctajc Posted September 2, 2009 Author Share Posted September 2, 2009 Correct Russell. 'each slot' being each date. Quote Link to comment https://forums.phpfreaks.com/topic/172774-combining-array-entries-with-same-date/#findComment-910641 Share on other sites More sharing options...
RussellReal Posted September 2, 2009 Share Posted September 2, 2009 okay... <?php $list = array(); $list[] = array('2008-01-01','120.00',''); $list[] = array('2008-02-09','','120.00'); $list[] = array('2008-01-01','','80.00'); // initial sorting loop.. $dates = array(); foreach ($list as $v) { if (!isset($dates[$v[0]])) { // if $dates['2008-01-01'] isn't set.. $dates[$v[0]] = array(); } $dates[$v[0]][] = array($v[1],$v[2]); } // once its all sorted BY DATE $oneLines = array(); foreach ($dates as $date => $array) { $actual = 0; $budget = 0; foreach ($array as $v) { $budget += $v[0]; $actual += $v[1]; } $oneLines[] = array($date,$budget,$actual); } print_r($oneLines); ?> Quote Link to comment https://forums.phpfreaks.com/topic/172774-combining-array-entries-with-same-date/#findComment-910653 Share on other sites More sharing options...
sctajc Posted September 2, 2009 Author Share Posted September 2, 2009 Thanks. Looks like I should be able to make it work. Will try to get back to it tomorrow Quote Link to comment https://forums.phpfreaks.com/topic/172774-combining-array-entries-with-same-date/#findComment-910839 Share on other sites More sharing options...
sctajc Posted September 2, 2009 Author Share Posted September 2, 2009 Great stuff Russell. Once I got the brain in gear had it working in 10 minutes. I learnt something. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/172774-combining-array-entries-with-same-date/#findComment-910857 Share on other sites More sharing options...
RussellReal Posted September 2, 2009 Share Posted September 2, 2009 no problemo click 'solved' tho Quote Link to comment https://forums.phpfreaks.com/topic/172774-combining-array-entries-with-same-date/#findComment-910860 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.