Summing durations associated with each MAC address in a MongoDB result
You can do this in only one foreach, making it faster and intelligible for future modification or reuse. The approach  I propose is as follows:

//(some sort of pythonish pseudocode)
// let cursor be the array with the mac addresses and duration
result = [] // empty array
for each element in cursor as document:
  document['mac'] is not a key of result then:
    result[ document['mac'] ] = document['duration']
  otherwise
    result[ document['mac'] ] += document['duration']


This way you can quickly check your data, it is very handy. The only difference is that you'll get an array like this:

[
   'mac1' =&gt; 'total duration in integer type',
   'mac2' =&gt; 'total duration in integer type for mac2'
   ... etc ..
]


In case you really need the current result array structure, you can do this after the first loop:

//(some sort of pseudocode)
arr = [] //initialize the array
for each element in result as mac =&gt; duration:
  arr[] = [ 'mac' =&gt; mac, 'assoc_time' =&gt; duration ]


The difference between nesting two (possibly) big loops and just checking if a key exists in an array is significant. Now let's write a few lines of PHP code:

&lt;?php
$result = array(); // maybe just $result = []; in case you have a proper PHP version.
foreach ($cursor as $document):
  if ( isset( $result[ $document['mac'] ] ) ):
    $result[ $document['mac'] ] += $document['duration']
  else:
    $result[ $document['mac'] ] = $document['duration']
  endif;
endforeach;


You can just replace the endforeach, endif with the traditional brackets.