title: How to organize in chunks a not accurate division?
body: <p>I have a number of participants and a number of groups, and I have to organize the participants into groups.</p>
  
  <p>Example:</p>
  
  <pre><code>10/3 = 3, 3 and 4.
  10/9 = 2,2,2 and 4.
  23/3 = 6,6,6 and 5.
  </code></pre>
  
  <p>I have tried with array_chunk using the size paramether as a rounded result of participants/groups but it Did not work well.</p>
  
  <p><strong>Edit</strong> with my problem solved.</p>
  
  <pre><code>    $groups           = $this-&gt;request-&gt;data['phases_limit'];
      $classified_lmt   = $this-&gt;request-&gt;data['classified_limit'];
  
      $participants     = count($game-&gt;user_has_game);
  
      $participants_lmt = floor($participants / $groups);
      $remainders       = $participants % $groups;
  
      if ($groups &gt; $participants) {
          throw new \Exception("Há mais grupos que participantes");
      }
  
      for ($i=0; $i &lt; $groups; $i++) {
          $p = $this-&gt;Phase-&gt;newEntity();
          $p-&gt;name             = 'Grupo #' . $game-&gt;id;
          $p-&gt;game_id          = $game-&gt;id;
          $p-&gt;classified_limit = $classified_lmt;
          $this-&gt;Phase-&gt;save($p);
  
          // add the number of participants per group
          for ($j=0; $j &lt; $participants_lmt; $j++) {
              $user_has_game = array_pop($game-&gt;user_has_game);
              $g                   = $this-&gt;Phase-&gt;GroupUserHasGame-&gt;newEntity();
              $g-&gt;group_id         = $p-&gt;id;
              $g-&gt;user_has_game_id = $user_has_game-&gt;id;
              $this-&gt;Phase-&gt;GroupUserHasGame-&gt;save($g);
          }
  
          // check if it is the last iteration
          if (($groups - 1) == $i) {
              // add the remainders on the last iteration
              for ($k=0; $k &lt; $remainders; $k++) {
                  $user_has_game  = array_pop($game-&gt;user_has_game);
                  $g                   = $this-&gt;Phase-&gt;GroupUserHasGame-&gt;newEntity();
                  $g-&gt;group_id         = $p-&gt;id;
                  $g-&gt;user_has_game_id = $user_has_game-&gt;id;
                  $this-&gt;Phase-&gt;GroupUserHasGame-&gt;save($g);
              }
          }
      }
  </code></pre>