title: print values using for loop within foreach loop
body: <p>I am facing trouble in printing values using for loop within foreach loop.
  I am populating dropdown list using foreach loop by defining an array and then marking the values as selected which matches with values retrieved from database but the problem is values are repeating in last dropdown. Here is my code:
  First I am converting string values retrieved from database into an array. and the code and output is as follows:</p>
  
  <pre><code>foreach($officeDetails as $value){ 
     $str = $value['days'];  
     $arr = explode(", ", $str); 
  }
  print_r($arr);
  </code></pre>
  
  <p>Output is:</p>
  
  <pre><code> Array ( 
          [0] =&gt; Monday 
          [1] =&gt; Tuesday 
        ) 
  Array ( 
          [0] =&gt; Wednesday 
          [1] =&gt; Thursday 
          [2] =&gt; Friday 
        )
  </code></pre>
  
  <p>Now I am trying to populate a dropdown list using foreach loop with an array called $daysArr(see below in code) and marking the values as selected which are retrieved from database as shown above in an array called $arr. 
  Code:</p>
  
  <pre><code> foreach($officeDetails as $value){ 
     $str = $value['days'];  
     $arr = explode(", ", $str); 
  
  &lt;select id="dw" name="days[0][]" class="full-width select2-offscreen" data-init-plugin="select2" multiple="" tabindex="-1"&gt;
      &lt;?php $daysArr = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
  
      foreach ($daysArr as $days) {
          for ($i = 0; $i &lt; count($arr); $i++) {
              if ($days == $arr[$i]) {
                  ?&gt;
                  &lt;option value="&lt;?php echo $days; ?&gt;" selected&gt;&lt;?php echo $days; ?&gt;&lt;/option&gt;
  
              &lt;?php } else { ?&gt;
                  &lt;option value="&lt;?php echo $days; ?&gt;"&gt;&lt;?php echo $days; ?&gt;&lt;/option&gt;
  
              &lt;?php } /* end else condition */
          }/* end for condition */
      } /*end foreach loop */
      }
      ?&gt;
  
  &lt;/select&gt;
  </code></pre>
  
  <p>So the result is; two select boxes are printed, showing right selected values but the second select box is printing each value of "$days" three times like Monday is showing three times in the dropdown list. </p>
  
  <p>I know its something minor that I am missing, please help me I am badly stuck in a loop and can't get out of it. Your suggestions are highly appreciated. Thanks in advance.</p>
  
  <p>Kind Regards.</p>