Jump to content

Combining array entries with same date


sctajc

Recommended Posts

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.

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.