title: Nest for each loop from two arrays where row of first loop signals next loop
body: <p>my main objective is to make a collapsible table in php/css/javascript (That I think I have sorted) but I am a little confused as to how to run a <code>for each</code> loop inside another (maybe a <code>while</code> loop?) so that it breaks out of the second loop. Basically, I have data in one array, and can use one of the values in the first array to get values from rows in the second array where they have the same value. Something like this;</p>
  
  <pre><code>Category 1
   - Book Name 1 - Author 3 - Date 1
   - Book Name 2 - Author 3 - Date 3
   - Book Name 4 - Author 2 - Date 4
   - Book Name 7 - Author 2 - Date 3
  Category 2
   - Book Name 3 - Author 1 - Date 2
   - Book Name 5 - Author 3 - Date 1
   - Book Name 8 - Author 1 - Date 5
   - Book Name 9 - Author 5 - Date 1
  </code></pre>
  
  <p>my first array has basic info;</p>
  
  <pre><code>([0] =&gt; Array [Category]=&gt;Category 1 [subscribers]=&gt;13 [reads]=&gt;4)
  ([1] =&gt; Array [Category]=&gt;Category 2 [subscribers]=&gt;13 [reads]=&gt;4)
  </code></pre>
  
  <p>and my second array is set up with rows like this:</p>
  
  <pre><code>([0] =&gt; Array [Category]=&gt;Category 1 [Name]=&gt;Book Name 1 [Author]=&gt;Author 3[Date]=&gt;Date 1)
  ([1] =&gt; Array [Category]=&gt;Category 1 [Name]=&gt;Book Name 2 [Author]=&gt;Author 3[Date]=&gt;Date 3)
  ([3] =&gt; Array [Category]=&gt;Category 2 [Name]=&gt;Book Name 3 [Author]=&gt;Author 1[Date]=&gt;Date 2)
  etc...
  </code></pre>
  
  <p>The common factor I can call is the category, so if I set it as a variable in the first while loop, would something like this work?</p>
  
  <pre><code>foreach (array1['category'] as $item){
  $category = array1['category']
  
       while(array2['category'] = $category){
       //do stuff
  }
  }
  </code></pre>
  
  <p><em>FWIW</em> I try to make my questions as generic as possible so other people can use them to adapt to their code more readily.  </p>