Sum of integer vaules of a multi value field using Computed Field Module I have a field that is configured to have unlimited values.  (field_a)

How can i use Computed field to add all of the integer values of A?

A: 10, 5, 2

The computed field would then spit out 17

This code will add the last values of field_a and field_b together:

$entity_field[0]['value'] = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_calories'))) + array_pop(array_pop(field_get_items($entity_type, $entity, 'field_calories')));


I just need to add all the values of A together.  Anyone know how?
I don't have computed field installed anywhere so I can't test this, but the logic should be something along the lines of:

$field_items = field_get_items($entity_type, $entity, 'field_calories');

$total = 0;
foreach ($field_items as $item) {
  $total += $item['value'];
}

$entity_field[0]['value'] = $total;


Or if you absolutely need a one-liner:

$entity_field[0]['value'] = array_sum(array_map(function($a) { return $a['value']; }, field_get_items($entity_type, $entity, 'field_calories')));