如何干净地迭代数组PHP

时间:2018-01-11 09:22:35

标签: php laravel

你好我很难迭代我的数组。我不知道如何使用简单foreach$key使用key。我尝试使用 @foreach($totals as $key => $total) @if($total['payroll_employee_company_id'] == $comid->payroll_company_id) <tr class="total"> <td class="text-center"><strong>TOTAL</strong></td> <td class="text-center"></td> <td class="text-center"></td> <td class="text-center">{{$total['loan_total']}}</td> <td class="text-center">{{$total['total_total_payment']}}</td> <td class="text-center">{{$total['total_remaining_balance']}}</td> <td class="text-center"></td> </tr> @endif @endforeach ,但我的错误是:

非法字符串偏移&#39; payroll_employee_company_id&#39; (查看:/var/www/html/digimahouse/resources/views/member/payrollreport/loan_summary_table.blade.php)

我如何成功迭代它?

这是我的阵列 enter image description here

这是我的预言

public function loan_summary()
{
    $data["page"] = "Loan Summary";
    $data["_loan_data"] = PayrollDeductionController::get_deduction($this->shop_id());
    $data["_company"] = Payroll::company_heirarchy(Self::shop_id());//Tbl_payroll_company::where("shop_id", Self::shop_id())->where('payroll_parent_company_id', 0)->get();
    $data['totals'] = $this->get_totals_loan_summary($data);

    return view("member.payrollreport.loan_summary", $data);
}

这是我的控制器

{{1}}

2 个答案:

答案 0 :(得分:1)

你说这张照片是{{ dd($totals) }}的结果。在这种情况下,请执行以下操作:

@foreach($totals['totals'] as $key => $total)
      @if($total['payroll_employee_company_id'] == $comid->payroll_company_id)
        <tr class="total">
          <td class="text-center"><strong>TOTAL</strong></td>
          <td class="text-center"></td>
          <td class="text-center"></td>
          <td class="text-center">{{$total['loan_total']}}</td>
          <td class="text-center">{{$total['total_total_payment']}}</td>
          <td class="text-center">{{$total['total_remaining_balance']}}</td>
          <td class="text-center"></td>
        </tr>
    @endif
@endforeach

答案 1 :(得分:0)

你的foreach不在正确的数组上

\0

@endforeach