title: PHP array group in loop and create multi dimensional array
body: <p>I have an associative multi dimensional array 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>I need to create new array based on similar element from above array. means I have to create array group. for e.g class element has repetitive 1styear and 2ndyear  value. so it shouls create array of unique element. then again class is parent array and inside class array there should be branch based array and inside brance Exam array and inside Exam array there should be associative array of student name and bio.</p>
  
  <p>so basically array should look like this </p>
  
  <pre><code>array(
      "1styear" =&gt; array(
          "IT" =&gt; array(
              "SEM1" =&gt; array(
                  array(
                      "student name" =&gt; "Alex",
                      "Bio" =&gt; "Good Boy"
                  ),
                  array(
                      "student name" =&gt; "Philip",
                      "Bio" =&gt; "Programmer"
                  )
              ),
              "SEM2" =&gt; array(
                  array(
                      "student name" =&gt; "Martin",
                      "Bio" =&gt; "Smart"
                  )
              )
          )
      ),
      "2ndyear" =&gt; array(
          "Finance" =&gt; array(
              "SEM1" =&gt; array(
                  array(
                      "student name" =&gt; "Mark",
                      "Bio" =&gt; "Intelligent"
                  )
              ),
              "SEM2" =&gt; array(
                  array(
                      "student name" =&gt; "Mike",
                      "Bio" =&gt; "Sport Player"
                  )
              )
          )
      )
  );
  </code></pre>
  
  <p>To make group based on class i did like below which is working fine but how to create array inside that </p>
  
  <pre><code>$classgroup = array();  
      foreach($data as $inarray){
  
           $classgroup[$inarray['class']][] = $inarray;
      }
      $classarray = array();
      foreach($classgroup as $key =&gt; $value){
              echo $key; // output is 1styear and secondyear 
              create array like above
      }
  </code></pre>
  
  <p><strong>---------------------------------EDIT----------------------------------</strong></p>
  
  <p>from the below loop </p>
  
  <pre><code>foreach($data as $array){
          $grouped[$array["class"]][$array["branch"]][$array["Exam"]][]=array("student name"=&gt;$array["student name"],"Bio"=&gt;$array["Bio"]);
  } 
  </code></pre>
  
  <p>i got expected o/p
  but if i need another o/p like this </p>
  
  <p>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>i tried following loop but not getting o/p as expected</p>
  
  <pre><code>foreach($data as $array){
          $grouped[$array["class"]][]=array("Exam"=&gt;$array["Exam"],"branch"=&gt;$array["branch"]);
  }
  </code></pre>