0

I am receiving an undefined offset: 1 error exception for a for-each loop when the input for the scheduling tool I am trying to fix is empty. I'm newish to PHP/Laravel and have been assigned to fix someone else's code unfortunately.

I have researched other posts are have experimented with issets and array_key_exists if loops but may have been using them incorrectly? The error points to the first line of the foreach loop posted in the image. Thanks for the input

@foreach ($headliners[$location->id] as $h)
      @if  ($h['count'] > 2)
          <th colspan="{{ $h['count'] }}"> {{ $h['headliner'] }}</th>
      @else
          <th colspan="{{ $h['count'] }}" class="vertical"><p>{{ $h['headliner'] }} </p></th>
      @endif
@endforeach
William M
  • 11
  • 1
  • Welcome to [so]. Please take a [tour] of the site, read [ask] and how to create a [mcve]. Then come back to your question and reformulate it (preferably with code samples, provided input and the expected output) in order to get a (useful) answer. Before posting a question, [search](/search) the site and make sure a similar question wasn't already answered. – axiac Aug 17 '17 at 22:45
  • If the error is really in first line of foreach loop, it likely means that $h variable doesn't contain 'count' field, likely is empty or null. Wrap that in @if (isset($h['count']) && $h['count'] > 2) – Dolfa Aug 17 '17 at 22:51
  • The @foreach loop you are using in template is similar to foreach in PHP, so behaviour is same, but your problem is not connected with loop itself but with `$headliners` var which doesn't have `$location-id`'th element. Wrap your loop into `@if (count($headliners) > $location->id) ... @endif` It will skip loop if there is none to loop through – iXCray Aug 17 '17 at 22:52

0 Answers0