Skip to content

how to get SUM of value from the collection?

I’m trying to get some of the values from the collection,
below is my code.

<?php 
public function RentalPartnerRevenue()
  { 
    $filter = $this->request->getParam('filter'); 
    $operator_id = $this->request->getParam('operator_id');   
    try{ 

        $completecollection1 = $this->_orderCollectionFactory->create()
                                             ->addAttributeToSelect('*');  
            
        if(isset($filter) && $filter  == 'totalrevenue'){
            //$completecollection1->addAttributeToSelect('grand_total')->getColumnValues('grand_total');       
            $completecollection1->getSelect()->joinLeft(
                'rider_detail as wrd',
                'wrd.booking_id = main_table.entity_id',
                ['entity_id','order_total', 'operator_id']
            )->order('wrd.created_at DESC')
             ->where('booking_status ="' . self::COMPLETED . '"')
             ->where('operator_id ="' . $operator_id . '"');

            echo $completecollection1->getSelect()->__toString();
            //print_r($completecollection1->getData());

        }elseif(isset($filter) && $filter  == 'todaysrevenue'){ 

        }elseif(isset($filter) && $filter  == 'lastmonthrevenue'){ 

        }elseif(isset($filter) && $filter  == 'lastthreemonthrevenue'){ 

        }elseif(isset($filter) && $filter  == 'selectcustomdate'){

        }
         
        if(count($completecollection1->getData())){   
           $PartnerArray = [];
            foreach ($completecollection1 as $Partnerrevenue) {  
             if(isset($PartnerArray[$Partnerrevenue->getData('operator_id')])){ 
                 $PartnerArray[$Partnerrevenue->getData('operator_id')] = $PartnerArray[$Partnerrevenue->getData('operator_id')] + $Partnerrevenue->getData('grand_total');                 
               }else{
                 $PartnerArray[$Partnerrevenue->getData('operator_id')] = $Partnerrevenue->getData('grand_total');
               } print_r($PartnerArray); // right data
            } 
            
        }
    }catch (Exception $e) {
        return $this->setResponse(500, $e->getMessage(), null);
    }
  }

OUTPUT

Array
(
[2707173] => 700.0000
)
Array
(
[2707173] => 7500
)
Array
(
[2707173] => 12900
)
Array
(
[2707173] => 18300
)
Array
(
[2707173] => 23700
)
Array
(
[2707173] => 25500
)
Array
(
[2707173] => 35500
)
Array
(
[2707173] => 45500
)
Array
(
[2707173] => 79500
)
Array
(
[2707173] => 113500
)

here 2707173 is operator_id
and => 700.000 to 113500 is order_total

now I want to get sum of order_total, but I don’t know how can I sum these values.
please help me with this.

thanks.