title: Managing user groups in php session
body: <p>I need to devide connected users into groups of 3 users each. I do not know how many will connect. Let's say there will be 10 users for test purpose. That means, there has to be 4 groups in session.
  Problem:  I cannot identify users. The only trigger is a button in form, which has a post method.</p>
  <p>I have started something to create, but can't figure it out, whether it is a right logic for this goal</p>
  <p><strong>index.php</strong></p>
  <pre><code>&lt;?php
    session_start();
  
    if (isset($_POST['join']) === true) {
      if (empty($_SESSION)) {
         $room = generateRandomString(25); // hash
         $_SESSION[$room]['users'] = 1;
      } else {
         $last_room = end($_SESSION);
         if ($last_room['users'] &lt; 3) {
            end($_SESSION)['users'] = $last_room['users'] + 1;
         } else {
            $new_room='';
            $_SESSION[$new_room] = array(generateRandomString(25));
            $_SESSION[$new_room]['users'] = 1;
         }
     }
     var_dump($_SESSION); // screenshot is below
   }
  ?&gt;
  </code></pre>
  <p><a href="https://i.stack.imgur.com/l62yK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l62yK.png" alt="enter image description here" /></a></p>
  <p>Every user which clicks the button on the same page gets only different hash and users = 1 with the code above.</p>
  <p>What I was trying to make is:</p>
  <pre><code>Array
  (
       [7YzEbT4q7FjrB2Y5SDNVZMHQk] =&gt; Array
           (
               [users] =&gt; 3
           )
       [f84gh304tu34g93ug9ug39g3g] =&gt; Array
           (
               [users] =&gt; 3
           )
  
       [ndmh5gh304tu34g93ug9ug3g4] =&gt; Array
           (
               [users] =&gt; 3
           )
  
       [56404tu34g93ug9ug3974tnm8] =&gt; Array
           (
               [users] =&gt; 1 // or 2 or 3, depends upon amount of connected users 
           )
   )
  </code></pre>
  <p>Where is an error?</p>