2

I am in need to do this thing: I have an object like below, but i am in need to do the sum of the numbers of objects like object1_(anything), object2_(anything)

stdClass Object
(
    [object1_2012_06_12] => 16
    [object2_2012_06_12] => 10
    [object1_2012_06_11] => 16
    [object2_2012_06_11] => 10
)

For examaple: sum of object1_(anything) will be (object1_2012_06_12 + object1_2012_06_11) = (16+16)=32

5
  • Sorry for been unconstructive here but I would strongly recommend you change that design to an array of objects. It doesn't sit well with OOP Commented Jun 12, 2012 at 12:53
  • means, there is no way to do this? Commented Jun 12, 2012 at 12:54
  • No it doesn't Mohan, it means your design makes it more complicated than it need be, not impossible Commented Jun 12, 2012 at 12:56
  • You design will work but Commented Jun 12, 2012 at 12:59
  • Your design will work but maintaining it will be difficult Commented Jun 12, 2012 at 12:59

3 Answers 3

8

You can cast your object to array:

$sum = 0;
foreach ((array)$myobj as $v) {
  $sum += intval($v);
}

Or as suggested by @MarkBaker:

$sum = array_sum((array)$myobj);
Sign up to request clarification or add additional context in comments.

5 Comments

I don't want to cast, i just want sum.
isn't $sum = array_sum((array) $myobj); even easier?
@MarkBaker man, I've been coding PHP for years and I missed that function. Thanks!
I have converted the object to array, but don't know how can I do the sum of [array_val_1_2012_06_12] => 16 and [array_val_1_2012_06_11] => 16. dates are dynamically generated. Is there any wildcard as we use in sql like %
Yea, it's called regular expressions.
0

Just go through the object attributes and sum based on part before first underscore using strtok:

$sums = array();
foreach ($my_object as $key => $value) {
    $key = strtok($key, '_');

    if (!isset($sums[$key])) {
            $sums[$key] = $value;
    } else {
            $sums[$key] += $value;
    }
}

print_r($sums);

Or:

function sum_of_object_starting_with($my_object, $starts_with)
{
    $sum = 0; $prefix_len = strlen($starts_with);
    foreach ($my_object as $key => $value) {
        if (strncmp($key, $starts_with, $prefix_len)) {
            $sum += $value;
        }
    }
    return $sum;
}

print_r(sum_of_object_starting_with($my_object, 'object1_'));

Comments

0

This code will get the value you want:

function sum_by_object_name ($data, $objName) {

  // Temporary array to hold values for a object name
  $objValues = array();

  // Convert input object to array and iterate over it
  foreach ((array) $data as $key => $val) {

    // Extract the object name portion of the key
    $keyName = implode('_', array_slice(explode('_', $key), 0, -3));

    // If object name is correct push this value onto the temp array
    if ($keyName == $objName) {
      $objValues[] = $val;
    }

  }

  // Return the sum of the temp array
  return array_sum($objValues);

}

// Calculate the total of $object
$total = sum_by_object_name($object, 'object1');

See it working

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.