如何在laravel 6中获取foreach中的数组总和

Posted

技术标签:

【中文标题】如何在laravel 6中获取foreach中的数组总和【英文标题】:How to get sum of array in foreach in laravel 6 【发布时间】:2021-01-09 06:54:06 【问题描述】:

我的控制器总和不起作用

public function show($id)


$workers = DB::table('workers')
->select('wrk_id','f_name','m_name','l_name')
   ;
$attendance = DB::table('attendance')->where('payroll_daily_id', $id)
->select('attendance.*','f_name','l_name','m_name',DB::raw('SUM(reg_hour)as tots'))
 
->rightjoinSub($workers,'worker', function($join)
    $join->on('attendance.wrk_id','=','worker.wrk_id');
)
->groupBy('payroll_attn_id')
->get();

  foreach($attendance as $key)
      $fetch[$key->wrk_id]['wrk_id'] = $key->wrk_id;
      $fetch[$key->wrk_id]['f_name'] = $key->f_name;
      $fetch[$key->wrk_id]['l_name'] = $key->l_name;
      $fetch[$key->wrk_id]['m_name'] = $key->m_name;
      $fetch['total_work_hours'] = $key->tots;
      $fetch[$key->wrk_id]['date'][$key->date]['work_hours'] = $key->reg_hour;
      
  
  return $fetch;  



这是result total_work_hours 仅显示8 而不是56

 2: wrk_id: 2, f_name: "John", l_name: "Doe", m_name: null,…
    date: 2020-09-27: work_hours: 8, 2020-09-28: work_hours: 8, 2020-09-29: work_hours: 8,…
    2020-09-27: work_hours: 8------------------
    2020-09-28: work_hours: 8------------------
    2020-09-29: work_hours: 8------------------
    2020-09-30: work_hours: 8------------------   TOTAL OF THIS IS 56
    2020-10-01: work_hours: 8------------------
    2020-10-02: work_hours: 8------------------
    2020-10-03: work_hours: 8-----------------
    f_name: "John"
    l_name: "Doe"
    m_name: null
    wrk_id: 2
    total_work_hours: "8" <-------------  56 is the total off work_hours from array

【问题讨论】:

你想为所有work_hours 获得 56 而不是 8? 是的,准确的...... 【参考方案1】:

像这样使用 foreach:

foreach($attendance as $key)
     if(empty($fetch[$key->wrk_id]['total_work_hours']))
          $fetch[$key->wrk_id]['total_work_hours'] = 0;
     
      $fetch[$key->wrk_id]['wrk_id'] = $key->wrk_id;
      $fetch[$key->wrk_id]['f_name'] = $key->f_name;
      $fetch[$key->wrk_id]['l_name'] = $key->l_name;
      $fetch[$key->wrk_id]['m_name'] = $key->m_name;
      $fetch['s'] = $key->tots;
      $fetch[$key->wrk_id]['total_work_hours'] += $key->reg_hour;   
  

【讨论】:

更新了答案。【参考方案2】:

试试这个

 $fetch = [];
    foreach($attendance as $key)
         if(!isset($fetch[$key->wrk_id]['total_work_hours']))
            $fetch[$key->wrk_id]['total_work_hours'] = 0;
          
          $fetch[$key->wrk_id]['wrk_id'] = $key->wrk_id;
          $fetch[$key->wrk_id]['f_name'] = $key->f_name;
          $fetch[$key->wrk_id]['l_name'] = $key->l_name;
          $fetch[$key->wrk_id]['m_name'] = $key->m_name;
          $fetch['s'] = $key->tots;
          $fetch[$key->wrk_id]['date'][$key->date]['work_hours'] = $key->reg_hour;
          $fetch[$key->wrk_id]['total_work_hours'] += $key->reg_hour;
          
      
      return $fetch;  

【讨论】:

不工作 work_hours 给了我总共 16 个 .. 我想得到导致 total_work_hours56 的所有 work_hours 的总数。 有错误Undefined index: total_work_hours", exception: "ErrorException" 请立即查看 你能帮我解决这个问题吗***.com/questions/64024707/…

以上是关于如何在laravel 6中获取foreach中的数组总和的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 6 - 循环并显示刀片中的值

如何在 Laravel 5.1 中获取数据库中的表列表

如何在刀片模板的 foreach 循环中访问 laravel 集合?

laravel - 无法在 foreach 之外获取数据

如何在laravel中的foreach循环中传递多个变量

如何使用 js 在刀片 laravel 中的 foreach 上更新变量