如何通过对相同ID的值进行分组来创建新数组

时间:2019-04-18 21:02:42

标签: php laravel laravel-5

我有一张桌子,上面有金额,director_id。我正在尝试通过添加相同的Director ID数量来创建数组,以便可以将其传递给Blade以创建表。但它会忽略每个ID的最后一个值。

贝勒是我的代码,但输出错误

$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)

        ;

表数组是这样的 我正在尝试为每个Director_id分组/添加金额

主表

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"
    }
  ]
}

我得到这个结果

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

但应该是

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

1 个答案:

答案 0 :(得分:1)

您将在整个循环中重复使用相同的$exp_amount变量,这就是为什么它会不断增大的原因。确实,您根本不需要它。只需将现有金额增加$exp对象中的金额即可。

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;
    } 
}

我认为您也可以为此使用收集方法。

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