tim peterson
tim peterson

Reputation: 24305

PHP arrays: summing values with matching dates

i'm trying to figure out how to sum certain values of a multi-dimensional array if they have similar dates.

Here's my array:

<?$myArray=array(

array(
        'year' => 2011,
        'month ' => 5,
        'day' => 13,
        'value' => 2
    ),
array(
        'year' => 2011,
        'month '=> 5,
        'day' => 14,
        'value' => 5
    ),
array(
        'year' => 2011,
        'month ' => 5,
        'day' => 13,
        'value' => 1
    ),
array(
        'year' => 2011,
        'month ' => 5,
        'day' => 14,
        'value' => 9
    )
);?>

here's how i'd like the output to look:

<?$output=array(

array(
        'year' => 2011,
        'month ' => 5,
        'day' => 13,
        'value' => 3 //the sum of 1+2
    ),
array(
        'year' => 2011,
        'month '=> 5,
        'day' => 14,
        'value' => 14 //the sum of 5+9
    )
);?>

Notice how the 4 sub-arrays were matched on year/month/day and then only the value was summed. I've seen other SO threads on this topic but can't find one where only the value is summed and not the year/month/day values too.

Thoughts?

Upvotes: 4

Views: 1826

Answers (2)

Jonathan
Jonathan

Reputation: 3034

Here's how I would do it. The result will be in $newArray with datetime objects as keys. If you just want it as an indexed array it should be pretty easy to do.

// Example array
$myArray = array(
    array(
    'date' => new DateTime('1993-08-11'),
    'value' => 3
    ),

    array(
    'date' => new DateTime('1993-08-11'),
    'value' => 5
    )
);

$newArray = array();

foreach($myArray as $element)
{
    $iterationValue = $element['value'];
    $iterationDate = $element['date'];
    $dateKey = $iterationDate->format('Y-m-d');

    if(array_key_exists($dateKey, $newArray))
    {
        // If we've already added this date to the new array, add the value
        $newArray[$dateKey]['value'] += $iterationValue;
    }
    else
    {
        // Otherwise create a new element with datetimeobject as key
        $newArray[$dateKey]['date'] = $iterationDate;
        $newArray[$dateKey]['value'] = $iterationValue;
    }
}

nl2br(print_r($newArray));

Actually ended up doing the pretty much the same thing as @MichaelBerkowski solution. Still, having DateTime objects is always more flexible when you wan't to do things with the dates later in your application.

Edit: Now tested it and fixed errors

Upvotes: 2

Michael Berkowski
Michael Berkowski

Reputation: 270637

It may be easiest to initially index your output array with a combination of the year/month/day:

Note: Your example array above has all its month keys with a trailing space. I'm just using month here with no trailing space.

// Initialize output array...
$out = array();

// Looping over each input array item
foreach ($myArray as $elem) {
  // Initialize a new element in the output keyed as yyyy-mm-dd if it doesn't already exist
  if (!isset($out[$elem['year'] . "-" . $elem['month '] . "-" . $elem['day']])) {
    $out[$elem['year'] . "-" . $elem['month '] . "-" . $elem['day']] = array(
      // Set the date keys...
      'year' => $elem['year'],
      'month' => $elem['month '],
      'day' => $elem['day'],
      // With the current value...
      'value' => $elem['value']
    );
  }
  // If it already exists, just add the current value onto it...
  else {
     $out[$elem['year'] . "-" . $elem['month '] . "-" . $elem['day']]['value'] += $elem['value'];
  }
}

// Now your output array is keyed by date.  Use array_values() to strip off those keys if it matters:
$out = array_values($out);

Outputs (before calling array_values()):

array(2) {
  '2011-5-13' =>
  array(4) {
    'year' =>
    int(2011)
    'month' =>
    int(5)
    'day' =>
    int(13)
    'value' =>
    int(3)
  }
  '2011-5-14' =>
  array(4) {
    'year' =>
    int(2011)
    'month' =>
    int(5)
    'day' =>
    int(14)
    'value' =>
    int(14)
  }
}

Update:

To do the same thing with single-key dates (rather than 3-parts) it is easier without the concatenation:

$myArray=array(
      array(
            'date' => '2011-05-13',
            'value' => 2
        ),
    array(
            'date' => '2011-05-14',
            'value' => 5
        ),
        array(
            'date' => '2011-05-13',
            'value' => 7
        ),
    array(
            'date' => '2011-05-14',
            'value' => 3
        ),

);

   foreach ($myArray as $elem) {
      // Initialize a new element in the output if it doesn't already exist
      if (!isset($out[$elem['date']])) {
        $out[$elem['date'] = array(
          // Set the date keys...
          'date' => $elem['date'],
          // With the current value...
          'value' => $elem['value']
        );
      }
      else {
        $out[$elem['date']]['value'] += $elem['value'];
      }
    }

Upvotes: 4

Related Questions