简体   繁体   中英

How to create a new array by grouping values for same id

I have a table where it has amount,director_id. I am trying create an array by adding amount of same Director id so that i can pass it to blade to create a table. but it ignoring the last values for each id.

Bellow is my code, but its gives wrong out put

$expense_details = DB::table('receipts')
            ->join('directors', 'directors.id', '=', 'receipts.director_id')
           ->join('account_type', 'account_type.id', '=', 'receipts.type')
            ->join('expense_type', 'expense_type.id', '=', 'receipts.exp_earn_type')

            ->select('receipts.*', 'directors.name as directors_name','account_type.name as account_name', 'expense_type.name as exp_name')
            ->get();

    dd($expense_details);



       $investor_total_by_id=[];

        $exp_amount=0;
      foreach ($expense_details as $exp){


          if (isset($investor_total_by_id[$exp->director_id])) {
              $exp_amount= $exp_amount + $exp->amount;
              $investor_total_by_id[$exp->director_id] = $exp_amount;

          }else {
              $investor_total_by_id[$exp->director_id] = $exp->amount;
          } 



       }

       //dd($investor_total_by_id);
        return view('Accounts/Investment/add')
             ->with('expense_details',$expense_details)
            ->with(' investor_total_by_id',  $investor_total_by_id)

        ;

the Table Array is like this I am trying to group/add amount for each director_id

Main table

Collection {#522 ▼
  #items: array:6 [▼
    0 => {#526 ▼
      +"id": 24
      +"type": 2
      +"exp_earn_type": 11
      +"sale_id": 0
      +"receipt_date": "2019-04-29 00:00:00"
      +"amount": 100.0
      +"description": "aesf"
      +"device_imei": "0"
      +"operator_id": 2
      +"director_id": 1
      +"emp_id": 0
      +"created_at": "2019-04-18 07:26:10"
      +"updated_at": "2019-04-18 07:26:10"
      +"directors_name": "Siam"
      +"account_name": "Expenses"
      +"exp_name": "Travel Expense"
    }
    1 => {#530 ▼
      +"id": 25
      +"type": 2
      +"exp_earn_type": 9
      +"sale_id": 0
      +"receipt_date": "2019-04-16 00:00:00"
      +"amount": 1000.0
      +"description": "ff"
      +"device_imei": "0"
      +"operator_id": 2
      +"director_id": 1
      +"emp_id": 0
      +"created_at": "2019-04-18 15:58:11"
      +"updated_at": "2019-04-18 15:58:11"
      +"directors_name": "Siam"
      +"account_name": "Expenses"
      +"exp_name": "GpsTrackingDevices"
    }
    2 => {#528 ▼
      +"id": 26
      +"type": 2
      +"exp_earn_type": 18
      +"sale_id": 0
      +"receipt_date": "2019-04-17 00:00:00"
      +"amount": 1000.0
      +"description": "ttt"
      +"device_imei": "0"
      +"operator_id": 2
      +"director_id": 1
      +"emp_id": 0
      +"created_at": "2019-04-18 17:45:54"
      +"updated_at": "2019-04-18 17:45:54"
      +"directors_name": "Siam"
      +"account_name": "Expenses"
      +"exp_name": "Computer – Hardware"
    }
    3 => {#527 ▼
      +"id": 27
      +"type": 2
      +"exp_earn_type": 13
      +"sale_id": 0
      +"receipt_date": "2019-04-15 00:00:00"
      +"amount": 5000.0
      +"description": "gg"
      +"device_imei": "0"
      +"operator_id": 2
      +"director_id": 1
      +"emp_id": 3
      +"created_at": "2019-04-18 21:01:37"
      +"updated_at": "2019-04-18 17:46:59"
      +"directors_name": "Siam"
      +"account_name": "Expenses"
      +"exp_name": "Payroll – Salary & Wages"
    }
    4 => {#525 ▼
      +"id": 28
      +"type": 2
      +"exp_earn_type": 17
      +"sale_id": 0
      +"receipt_date": "2019-04-15 00:00:00"
      +"amount": 222.0
      +"description": "ddd"
      +"device_imei": "0"
      +"operator_id": 2
      +"director_id": 2
      +"emp_id": 0
      +"created_at": "2019-04-18 19:22:36"
      +"updated_at": "2019-04-18 19:22:36"
      +"directors_name": "Riad"
      +"account_name": "Expenses"
      +"exp_name": "Rent Expense"
    }
    5 => {#529 ▼
      +"id": 29
      +"type": 2
      +"exp_earn_type": 16
      +"sale_id": 0
      +"receipt_date": "2019-04-11 00:00:00"
      +"amount": 222.0
      +"description": "22"
      +"device_imei": "0"
      +"operator_id": 2
      +"director_id": 2
      +"emp_id": 0
      +"created_at": "2019-04-18 19:22:54"
      +"updated_at": "2019-04-18 19:22:54"
      +"directors_name": "Riad"
      +"account_name": "Expenses"
      +"exp_name": "Office Supplies"
    }
  ]
}

I am getting this result

array:2 [▼
  1 => 7000.0
  2 => 7222.0
]

but it should be

array:2 [▼
  1 => 7100.0
  2 => 444.0
]

You're reusing the same $exp_amount variable throughout the entire loop, that's why it just keeps getting larger. Really you shouldn't need it at all, though. Just increment the existing amount by the amount from the $exp object.

foreach ($expense_details as $exp){
    if (isset($investor_total_by_id[$exp->director_id])) {
        $investor_total_by_id[$exp->director_id] += $exp->amount;
    } else {
        $investor_total_by_id[$exp->director_id] = $exp->amount;
    } 
}

I think you could also use collection methods for this.

$investor_total_by_id = $expense_details->groupBy('director_id')->map(function ($expenses) {
    return $expenses->sum('amount'); 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM