2016-01-09 141 views
-2

我有以下陣列:Laravel通過陣列刀片循環

$days = [ 
    ['dow' => 1, 'day' => 'Monday'], 
    ['dow' => 2, 'day' => 'Tuesday'], 
    ['dow' => 3, 'day' => 'Wednesday'], 
    ['dow' => 4, 'day' => 'Thursday'], 
    ['dow' => 5, 'day' => 'Friday'], 
    ['dow' => 6, 'day' => 'Saturday'], 
    ['dow' => 7, 'day' => 'Sunday'], 
]; 

我通過它試圖循環在我的刀模板如下:

@foreach ($days as $day) 
     <label>{{$day['day']}}</label> 
     {!! Form::text('day_of_wk[$day['dow']].start_time', null, ['class' => 'form-control'])!!} 
    @endforeach 

,但我得到的錯誤:syntax error, unexpected 'dow' (T_STRING)

如何在我的day_of_wk []輸入數組中添加$day['dow']值?

+0

看看他們周圍的報價。有一個語法錯誤。刪除單引號或用雙引號替換它們。 – Stony

+0

已經嘗試過,它不起作用。我認爲在使用laravel集體時,你不能以這種方式添加變量,除非有人能以其他方式顯示我。 – adam78

+0

當我刪除單引號時,我只需要'$ day [dow]'而不是實際值 – adam78

回答

2
@foreach ($days as $day) 
     <label>{{$day['day']}}</label> 
     {!! Form::text('day_of_wk['.$day['dow'].'].start_time', null, ['class' => 'form-control'])!!} 
    @endforeach 

@foreach ($days as $day) 
      <label>{{$day['day']}}</label> 
      {!! Form::text("day_of_wk[{$day['dow']}].start_time", null, ['class' => 'form-control'])!!} 
     @endforeach