Sorting arrays and comparing array values php

0👍

Try the below code:

$stats = DB::table('wallet_payouts')
        ->groupBy('date')
        ->orderBy('date', 'ASC')
        ->get([
            DB::raw('DATE_FORMAT(created_at, "%M") as date'),
            DB::raw('COUNT(*) as value')
        ]);

$labels = $months;
$data = [];
$temp = [];
foreach ($stats as $stat){
    $temp[$stat->date] = $stat->value;
}
foreach($months as $month){
    if(array_key_exists($month,$temp)){
        array_push($data, $temp[$month]);
    } else {
        array_push($data, 0);
    }
}

Leave a comment