title: Divide N elements in 3 columns by columns
body: <p>I need to write N elements (1 .. N) into 3 columns and need to get 3 values of elements, which are last in each column.</p>
  
  <p>I need to get this:</p>
  
  <pre><code> N ... array I need to get
   --------------------
   1 ... array(1)       // or array(1, 1, 1)
   2 ... array(1, 2)    // or array(1, 2, 2)
   3 ... array(1, 2, 3)
   4 ... array(2, 3, 4)
   5 ... array(2, 4, 5)
   6 ... array(2, 4, 6)
   7 ... array(3, 5, 7)
   8 ... array(3, 6, 8)
   9 ... array(3, 6, 9)
  10 ... array(4, 7, 10)
  11 ... array(4, 8, 11)
  12 ... array(4, 8, 12)
  13 ... array(5, 9, 13)
  ...etc
  </code></pre>
  
  <p>f.e., if <code>N = 10</code>, I need to get result <code>array(4, 7, 10)</code> and the output table will be like this:</p>
  
  <pre><code>1    5    8
  2    6    9
  3    7    10
  4    
  </code></pre>
  
  <p><strong>PS:</strong>
  not like this:</p>
  
  <pre><code>1    5    9
  2    6    10
  3    7    
  4    8
  </code></pre>
  
  <p>I wrote this code, but it has problems with <code>N = 10</code>. Could someone help me or find there a problem in that big condition? Or just make it simplier? Many, many thanks.</p>
  
  <pre><code>$count_all = 10; // this is N = 10
  
  $count = floor(($count_all - 1) / 3) + 1;
  
  $i = 0;
  $j = 1;
  
  echo '&lt;div style="float: left;"&gt;';
      for ($k = 1; $k &lt;= 10; $k++) {
          echo '&lt;div style="width: 230px;"&gt;';
              echo $k;
          echo '&lt;/div&gt;';
  
          if (
                          (++$i == $count) or
                          (($i == 1) and ($count_all - $i + 1 == $count)) or
                          (($i &gt; 1) and ($count_all - $i == $count)) or
                          ($i + ($j - 1) * $count == $count_all)
          )  {
              echo '&lt;/div&gt;';
              echo '&lt;div style="float: left;"&gt;';
  
              $i = 0;
              $j++;
          }
      }
  echo '&lt;/div&gt;';
  </code></pre>