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?

1 Answer 1


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')));
  • The first code snippet worked perfectly for me. Thanks!
    – hockey2112
    Dec 6, 2012 at 18:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.