title: Foreach loop displaying values in a certain format
body: <p>I have two foreach loops in which are fetching results from mysql database table. The values are displaying correctly but I am trying to compress the results so it doesn’t repeat values. For example there are two <code>courses</code> and two <code>instructors</code> in the table. That means when I run the loop I expect for a total of 4 results to display. But after running through the loop a total of 8 results are shown. But the extra results are a repetition. It possible to loop through completely one foreach loop before going to the next? Or what is the best way so it displays exactly the 4 results?</p>
  
  <pre><code>$db_select  = $db_con-&gt;prepare("
  SELECT      a.academy_id,
             ca.course_name,
             ca.course_start_date,
             ca.course_end_date,
             p.first_name,
             p.last_name
      FROM academy a
      INNER JOIN courses_by_academy ca ON a.academy_id = ca.academy_id
      INNER JOIN person p ON a.academy_id = p.academy_id
      WHERE a.academy_id = 123
  ");
  if (!$db_select) return false;
      if (!$db_select-&gt;execute(array('123' =&gt; $acad_id))) return false;
      $results = $db_select-&gt;fetchAll(\PDO::FETCH_ASSOC);
      if (empty($results)) return false;
      $final_result = '';
      $final_result2 = '';
  
      foreach ($results as $value){
            $final_result .= "-------------------COURSES_OFFERED------------------&lt;/br&gt;";
            $final_result .= "&lt;b&gt;Course Name: &lt;/b&gt;".$value['course_name']."&lt;/br&gt;&lt;b&gt;Start Date: &lt;/b&gt;".$value['course_start_date']."&lt;/br&gt;&lt;b&gt;End Date: &lt;/b&gt;".$value['course_end_date']."&lt;/br&gt;";      
      }
  
      foreach ($results as $value2){
            $final_result2 .= "---------------------STAFF-----------------------&lt;/br&gt;";
            $final_result2 .= "&lt;b&gt;First Name: &lt;/b&gt;".$value2['first_name']."&lt;/br&gt;&lt;b&gt;Last Name: &lt;/b&gt;".$value2['last_name']."&lt;/br&gt;";
      }
  
  }
  </code></pre>
  
  <p>Current Echo Display of results:</p>
  
  <pre><code>-------------------COURSES_OFFERED------------------
  Course Name: Biology
  Start Date: 2013-11-15
  End Date: 2013-11-27
  -------------------COURSES_OFFERED------------------
  Course Name: Biology
  Start Date: 2013-11-15
  End Date: 2013-11-27
  -------------------COURSES_OFFERED------------------
  Course Name: Calculus
  Start Date: 2013-11-16
  End Date: 2013-11-30
  -------------------COURSES_OFFERED------------------
  Course Name: Calculus
  Start Date: 2013-11-16
  End Date: 2013-11-30
  --------------------STAFF-----------------------
  First Name: Person1
  Last Name: Last1
  ---------------------STAFF-----------------------
  First Name: Person2
  Last Name: Last2
  ---------------------STAFF-----------------------
  First Name: Person2
  Last Name: Last2
  ---------------------STAFF-----------------------
  First Name: Person1
  Last Name: Last1
  </code></pre>
  
  <p>Seeking for a results like this:</p>
  
  <pre><code>-------------------COURSES_OFFERED------------------
  Course Name: Biology
  Start Date: 2013-11-15
  End Date: 2013-11-27
  -------------------COURSES_OFFERED------------------
  Course Name: Calculus
  Start Date: 2013-11-16
  End Date: 2013-11-30
  --------------------STAFF-----------------------
  First Name: Person1
  Last Name: Last1
  ---------------------STAFF-----------------------
  First Name: Person2
  Last Name: Last2
  </code></pre>