title: PHP array group in loop and create multi dimensional associative array
body: <p>I have array named data as below</p>
  
  <pre><code>$data = array();
  $data = array ( 
      [0] =&gt; Array (
                  [class]         =&gt; 1styear 
                  [branch]        =&gt; IT 
                  [Exam]          =&gt; SEM1 
                  [student name]  =&gt; Alex 
                  [Bio]           =&gt; Good Boy
                   )
      [1] =&gt; Array ( 
                  [class]         =&gt; 2ndyear 
                  [branch]        =&gt; Finance 
                  [Exam]          =&gt; SEM1 
                  [student name]  =&gt; Mark
                  [Bio]           =&gt; Intelligent 
                  )
      [2] =&gt; Array (
                   [class]        =&gt; 2ndyear 
                   [branch]       =&gt; IT 
                   [Exam]         =&gt; SEM1 
                   [student name] =&gt; Shaun 
                   [Bio]          =&gt; Football Player
                    ) 
      [3] =&gt; Array ( 
                  [class] =&gt; 1styear 
                  [branch] =&gt; Finance 
                  [Exam] =&gt; SEM2 
                  [student name] =&gt; Mike 
                  [Bio] =&gt; Sport Player 
                  ) 
      [4] =&gt; Array ( 
                  [class] =&gt; 1styear 
                  [branch] =&gt; IT 
                  [Exam] =&gt; SEM2 
                  [student name] =&gt; Martin 
                  [Bio] =&gt; Smart 
                   )
      [5] =&gt; Array ( 
                  [class] =&gt; 1styear 
                  [branch] =&gt; IT 
                  [Exam] =&gt; SEM1 
                  [student name] =&gt; Philip 
                  [Bio] =&gt; Programmer
                    )
      ) 
  </code></pre>
  
  <p>class,branch ,exam can be repetitive<br>
  I need to group above elements and create nested multi dimensional associative array . array should look like this
   expected o/p</p>
  
  <pre><code>array(
      '1styear' =&gt;
          array (
              0 =&gt;
                  array(
                      'Exam' =&gt; 'SEM1',
                      'branch' =&gt;
                          array (
                              0 =&gt; 'IT'
                          ),
                  ),
              1 =&gt;
                  array(
                      'Exam' =&gt; 'SEM2',
                      'branch' =&gt;
                          array (
                              0 =&gt; 'IT'
                          ),
                  ),
          ),
      '2ndyear' =&gt;
          array (
              0 =&gt;
                  array(
                      'Exam' =&gt; 'SEM1',
                      'branch' =&gt;
                          array (
                              0 =&gt; 'Finance',
                          ),
                  ),
              1 =&gt;
                  array(
                      'Exam' =&gt; 'SEM2',
                      'branch' =&gt;
                          array (
                              0 =&gt; 'Finance'
                          ),
                  )
          ),
  )
  </code></pre>
  
  <p>in  loop i tried following code</p>
  
  <pre><code>foreach($data as $array){
      $rr[$array["class"]]['Exam'][$array["Exam"]][] = array("Competitors" =&gt;$array["Competitor"]) ;
  }
  </code></pre>