You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用foreach生成周时间表遇参数类型错误,请求排查解决

错误排查:foreach()参数类型错误(字符串代替数组/对象)

问题根源是循环变量和数组变量重名,导致数组被覆盖成字符串:

在视图的<thead>部分,你写了@foreach($time as $time),第一次循环时,原本是数组的$time会被替换成当前循环的字符串元素(比如第一个循环后$time变成'09.00 - 10.00')。当后续在<tbody>里再次尝试遍历$time时,它已经是字符串,无法作为foreach的参数,直接触发类型错误。

修正方案:

把循环变量改成和数组变量不同的名称,比如$t或者$slot,避免变量覆盖。

修正后的完整视图代码:

<table class="table table-bordered">
    <thead>
        <th width="125">Time</th>
        @foreach($time as $t)
        <th>{{ $t }}</th>
        @endforeach
    </thead>
    <tbody>
        @foreach($weekDays as $day)
        <tr>
            <td>
                {{$day}}
            </td>
            @foreach($time as $t)
            <td>
                Hari : {{$day}} <br>
                Jam : {{$t}}
            </td>
            @endforeach
        </tr>
        @endforeach
    </tbody>
</table>

内容的提问来源于stack exchange,提问作者M. Rivaldi Anwar Putra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 20:52:14