I cannot get proper results from View PHP field.

I've set up a Flipped Table view of entity form submission of which I'm trying to calculate "results" (sum and count(for avg)) with following code

$results = array(); // For results
$static = get_object_vars($row);
foreach($static as $key => $value) {
    if(strpos($key,'field_') !== FALSE){
        if($value !== NULL){
            $results[$static['entityform_id']]['count']++; // Count no. of fields
            $results[$static['entityform_id']]['sum']= $results[$static['entityform_id']]['sum']+$value; // Count sum of field values
return json_encode($results[$static['entityform_id']]);

which results:

{"count":55,"sum":55}   {"count":55,"sum":110}  {"count":55,"sum":165}  {"count":55,"sum":220}

At this point I do not understand that why sum is always row index * sum? Eg.1 =55, 2=110, 3=165... All the submissions have the same fields and field values (clones) which explaines sums but I can't figure out that where this multiplication "logic" happens?

It seems like it is looping/counting all four result rows every time.

1 Answer 1


This is because $results[$static['entityform_id']]['sum'] already contains the sum result from previous loop result. Thus it adds 55 for every row iteration. Try adding the current array index to $results inside foreach loop.

  • How can it contain since entityform_id is different(1...4) and being used as an array index to separate calculated values for each submission? It should create new index and therefore value should be empty. But I'll try to use some other method for indexes and we'll see what then happens... Sep 2, 2015 at 16:39
  • Correct me if i am wrong.. How can entity form id be different if you are iterating over the submissions of a particular form.. Sep 2, 2015 at 17:17
  • Yeah, I know. That's a bit misleading. In Views guidelines it is named like that as "entityform id" but the description states that it is actually submission ID. And I did verify it by debugging. Sep 2, 2015 at 19:00
  • Okay, now that I tried to debug the actual field values it shows that all values are overriden. For the first submission value is 1 and so on until 4 (submission id). And to get these results I'm only using print function to view $row variable and not modifying any variable. Really weird... Sep 3, 2015 at 9:25
  • Did you try creating views handlers for your entity fields ?? That way you can get more flexibility with operations and customizations on the fields. Sep 3, 2015 at 17:57

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.