title: PHP rotate matrix counter-clockwise
body: <p>I am solving a larger problem and at one step I need to rotate a 2D array counter-clockwise.</p>
  
  <p>So if I have this matrix:</p>
  
  <pre><code>1 2 3 4
  1 2 3 4
  3 4 5 6
  3 4 5 6
  </code></pre>
  
  <p>After the rotation it will be:</p>
  
  <pre><code>4 4 6 6
  3 3 5 5
  2 2 4 4
  1 1 3 3
  </code></pre>
  
  <p>I have found a <a href="https://stackoverflow.com/a/3137413/407650">solution</a> to rotate it clockwise:</p>
  
  <pre><code>&lt;?php    
  $a = array(array(1,2,3,4),array(5,6,7,8),array(9,0,1,2),array(3,4,5,6));
  $b = array(); //result
  
  while(count($a)&gt;0)
  {
      $b[count($a[0])-1][] = array_shift($a[0]);
      if (count($a[0])==0)
      {
           array_shift($a);
      }
  }
  ?&gt;
  </code></pre>
  
  <p>The thing is that this has to work even when <code>a</code> is uni-dimensional or has only one element. </p>
  
  <p>So, <code>1 2 3 4</code> will become:</p>
  
  <pre><code>4
  3
  2
  1
  </code></pre>