title: Checking if a value is set within a PHP for loop using Laravel
body: <p>Using Laravel, I want to loop through a series of grades (from '0' through '12') and, if a grade is found in my intermediate/pivot table, I want the class for that specific matching item to append 'active'.</p>
  
  <p>(For example's sake, within my intermediate table, I have set it to have two active <strong>grade_id</strong>s: 6 and 7.)</p>
  
  <p>I tried a for loop:</p>
  
  <pre><code>@for ($x=0; $x&lt;=12; $x++)
      @if ($x === 0)
          &lt;button class="circular small ui icon button"&gt;K&lt;/button&gt;
      @else
          &lt;button class="circular small ui icon button"&gt;{{ $x }}&lt;/button&gt;
      @endif
  @endfor
  </code></pre>
  
  <p>And this loops nicely through the grades from 0 (K) to 12 just fine. But then how do I make <em>active</em> the items that match in the DB? I need to somehow add a foreach to output the $grades as $grade (or similar) and check if $x = $grade.</p>
  
  <p>But if I try an idea like that:</p>
  
  <pre><code>@for ($x=0; $x&lt;=12; $x++)
      @if ($x === 0)
          &lt;button class="circular small ui icon button"&gt;K&lt;/button&gt;
      @else
          @foreach ($grades as $key=&gt;$value)
              &lt;button class="circular small ui icon button"&gt;{{ $value }}&lt;/button&gt;
          @endforeach
      @endif
  @endfor
  </code></pre>
  
  <p>the logic mucks things up by outputting K, 6, 7 with the 6, 7 being repeated 12 times. Yup, that's what I have effectively told it to do.</p>
  
  <p>And doing it this way obviously doesn't work because I am only going to get a result that equals the number of rows matching in my DB:</p>
  
  <pre><code>&lt;?php $x = 0; ?&gt;
  @foreach ($grades as $grade)
      @if ($x === 0)
          &lt;button class="circular small ui icon button"&gt;K&lt;/button&gt;
      @else
          &lt;button class="circular small ui icon button"&gt;{{ $x }}&lt;/button&gt;
      @endif
      &lt;?php $x++; ?&gt;
  @endforeach
  </code></pre>
  
  <p>So, how do I output each/all of the grades (from 0 - 12) while checking if each one matches the value (grade_id) set in the intermediate database? I am guessing that this is a pretty simple task but I cannot think of what the control structure might be called that I can Google or search SO for...</p>
  
  <p><em>[Note that I have not included the table structure as I don't think this is relevant to the question.]</em></p>