title: How to split an array based on a condition?
body: <p>I'm working on a PHP/MySQL app and I have a very simple table that looks like this:</p>
  
  <pre><code>id | name | came_with
  1    John    null
  2    Jane    1
  3    Dean    null
  </code></pre>
  
  <p>It has about 50 records but the main idea is that it stores people who register for an event, if they bring guests their name also gets stored with the <code>id</code> of the person who invited them in the <code>came_with</code> field, very simple.</p>
  
  <p>I have to shuffle these names up and split them in X number of groups based on how many persons I want in each group which would be very easy since I can store each record in an array, use <code>shuffle</code> to randomize the order and the use <code>array_chunk</code> to generate smaller arrays depending on the number of persons.</p>
  
  <p>The problem is that I can't have people who came together on the same group, so in this example John and Jane can't be on the same group, it's ok though for a person who came with 2 more to get placed on the same group if I choose to generate 2 groups since there's no choice. I'm VERY lost in what the logic should be to try to do this, I'm not asking for the exact code but for ideas in how to accomplish it.</p>
  
  <p>Thanks in advance!</p>