1

How to Sum values in below array that have same key value in laravel controller I want to sum value in same date in key['x'] and display on group by Date

This controller

        $MB = DB::table('tbPCSInfo')
        ->distinct()
        ->where('UserID','=',1)
        ->select('MBxID')
        ->get();

    foreach($MB as $mbxid)
    {

    $Inv = tbPCSInfo::with('tbPcsStatusDescripts')
        ->distinct()
        ->where('MBxID', '=', $mbxid->MBxID)
        ->select('PcsID')                    
        ->get(); 

    foreach($Inv as $pcsid)
            {
                $data[] = DB::table('tbPCSInfo')
                ->whereYear('Time' ,'=',2018)  
                ->where('MBxID' ,'=',$mbxid->MBxID)
                ->where('PcsID' ,'=', $pcsid->PcsID)          
                ->whereMonth('Time' ,'=',2)
                ->select(DB::raw('UNIX_TIMESTAMP(DATE(Time))*1000 AS x 
,MAX(PConsumption) - MIN(PConsumption) AS y '))
                // ->select(DB::raw('UNIX_TIMESTAMP(DATE(Time))*1000 AS x'))
                // ->select(DB::raw('MAX(PConsumption) - MIN(PConsumption) AS 
y'))    
                // ->select(DB::raw('MAX(PConsumption)- min AS y'))
                ->groupBy(DB::raw('UNIX_TIMESTAMP(DATE(Time))*1000') )
                // ->groupBy('x')
                ->get();   
            }                 
    }
    dump($data);   

This array from laravel controller this I want to sum value in same date in key['x'] and display on group by Date

    array:3 [▼
0 => Collection {#1360 ▼
#items: array:5 [▼
  0 => {#1292 ▼
    +"x": "1518973200000"
    +"y": "1"
  }
  1 => {#1293 ▼
    +"x": "1519059600000"
    +"y": "112"
  }
  2 => {#1294 ▼
    +"x": "1519146000000"
    +"y": "98"
  }
  3 => {#1295 ▼
    +"x": "1519232400000"
    +"y": "86"
  }
  4 => {#1296 ▼
    +"x": "1519318800000"
    +"y": "18"
  }
]
}
1 => Collection {#1461 ▼
#items: array:5 [▼
  0 => {#1393 ▼
    +"x": "1518973200000"
    +"y": "1"
  }
  1 => {#1394 ▼
    +"x": "1519059600000"
    +"y": "111"
  }
  2 => {#1395 ▼
    +"x": "1519146000000"
    +"y": "100"
  }
  3 => {#1396 ▼
    +"x": "1519232400000"
    +"y": "86"
  }
  4 => {#1397 ▼
    +"x": "1519318800000"
    +"y": "18"
  }
]

And I hope to see result like this (//Sum of y in array group by x )

array [▼
  0 => {#1292 ▼
    +"x": "1518973200000"
    +"y": "??" //Sum of y in array group by x  
  }
  1 => {#1293 ▼
    +"x": "1519059600000"
    +"y": "112" //Sum of y in array group by x  
  }
  2 => {#1294 ▼
    +"x": "1519146000000"
    +"y": "98" //Sum of y in array group by x  
  }
  3 => {#1295 ▼
    +"x": "1519232400000"
    +"y": "86" //Sum of y in array group by x  
  }
  4 => {#1296 ▼
    +"x": "1519318800000"
    +"y": "18" //Sum of y in array group by x  
  }

and this array result enter image description here

1

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.