php array foreach loop data manipulation -
i have script gets data mongodb in form of array , show results. array has many records. sample array follows:
array(2) { ["mac"]=> string(17) "2c:33:7a:10:f8:39" int(1478199995) ["duration"]=> int(5) } array(2) { ["mac"]=> string(17) "38:0b:40:ad:03:53" int(1478203338) ["duration"]=> int(3) } array(2) { ["mac"]=> string(17) "38:0b:40:ad:03:53" int(1478201111) ["duration"]=> int(7) } array(2) { ["mac"]=> string(17) "2c:33:7a:10:f8:39" int(1478206709) ["duration"]=> int(7) } array(2) { ["mac"]=> string(17) "38:0b:40:ad:03:53" int(1478202821) ["duration"]=> int(6) } array(2) { ["mac"]=> string(17) "2c:33:7a:10:f8:39" int(1478202366) ["duration"]=> int(4) } array(2) { ["mac"]=> string(17) "38:0b:40:ad:03:53" int(1478205023) ["duration"]=> int(2) }
i showing record respect mac address in above array mac address "2c:33:7a:10:f8:39" has 3 records , "38:0b:40:ad:03:53" has four, add duration of each record , show on browser this
2c:33:7a:10:f8:39 16sec 38:0b:40:ad:03:53 18sec
i doing following logic think slow 5000 6000 records.
i first mac address in array , remove duplication , run loop on array , add duration specific mac code below.
<?php foreach ($cursor $document) { array_push($arr,$document["mac"]); } $arr=array_unique($arr); $duration=0; for($i=0;$i<count($arr);$i++){ foreach ($cursor $document) { if($document["mac"]==$arr[$i]) { $duration+=$document['assoc_time']; } echo $arr[$i]." ".$duration; } ?>
how can make process fast , how perform task in 1 foreach loop.
you should let mongodb summing. mdb query missing, see https://docs.mongodb.com/v3.2/reference/operator/aggregation/sum/
Comments
Post a Comment