title: Convert flat array to multidimensional array with dynamic keys. Is it possible?
body: <p>First of all, I have following data returned from database. I will have two different data as below respectively</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: right;">sum1</th>
  <th style="text-align: right;">count1</th>
  <th>sm__state_name__</th>
  <th style="text-align: right;">om__order_date__year</th>
  <th style="text-align: right;">om__order_date__quarter</th>
  <th style="text-align: right;">om__order_date__month</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: right;">5645000</td>
  <td style="text-align: right;">4</td>
  <td>Luanda</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">8</td>
  </tr>
  <tr>
  <td style="text-align: right;">213985939.8600001</td>
  <td style="text-align: right;">1606</td>
  <td>Luanda</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: right;">7729331.52</td>
  <td style="text-align: right;">119</td>
  <td>Benguela</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: right;">1012936</td>
  <td style="text-align: right;">17</td>
  <td>Zaire</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: right;">1054883</td>
  <td style="text-align: right;">19</td>
  <td>Bie</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: right;">2347944</td>
  <td style="text-align: right;">26</td>
  <td>Cuando Cubango</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: right;">428769.6000000001</td>
  <td style="text-align: right;">60</td>
  <td>Bengo</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: right;">6444569</td>
  <td style="text-align: right;">86</td>
  <td>Huila</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: right;">4914030</td>
  <td style="text-align: right;">25</td>
  <td>Cunane</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: right;">1167200</td>
  <td style="text-align: right;">26</td>
  <td>Cuanza North</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: right;">750080</td>
  <td style="text-align: right;">10</td>
  <td>Cuanza Sul</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: right;">2178100</td>
  <td style="text-align: right;">6</td>
  <td>Huambo</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: right;">1099934</td>
  <td style="text-align: right;">25</td>
  <td>Lunda North</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: right;">410135</td>
  <td style="text-align: right;">12</td>
  <td>Malange</td>
  <td style="text-align: right;">2017</td>
  <td style="text-align: right;">3</td>
  <td style="text-align: right;">9</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>In array format</p>
  <pre><code>   array (
    0 =&gt; 
    array (
      'sum1' =&gt; '5645000',
      'count1' =&gt; '4',
      'sm__state_name__' =&gt; 'Luanda',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '8',
    ),
    1 =&gt; 
    array (
      'sum1' =&gt; '213985939.8600001',
      'count1' =&gt; '1606',
      'sm__state_name__' =&gt; 'Luanda',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    2 =&gt; 
    array (
      'sum1' =&gt; '352839.60000000003',
      'count1' =&gt; '9',
      'sm__state_name__' =&gt; NULL,
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    3 =&gt; 
    array (
      'sum1' =&gt; '7729331.52',
      'count1' =&gt; '119',
      'sm__state_name__' =&gt; 'Benguela',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    4 =&gt; 
    array (
      'sum1' =&gt; '1012936',
      'count1' =&gt; '17',
      'sm__state_name__' =&gt; 'Zaire',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    5 =&gt; 
    array (
      'sum1' =&gt; '1054883',
      'count1' =&gt; '19',
      'sm__state_name__' =&gt; 'Bie',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    6 =&gt; 
    array (
      'sum1' =&gt; '2347944',
      'count1' =&gt; '26',
      'sm__state_name__' =&gt; 'Cuando Cubango',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    7 =&gt; 
    array (
      'sum1' =&gt; '428769.6000000001',
      'count1' =&gt; '60',
      'sm__state_name__' =&gt; 'Bengo',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    8 =&gt; 
    array (
      'sum1' =&gt; '6444569',
      'count1' =&gt; '86',
      'sm__state_name__' =&gt; 'Huila',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    9 =&gt; 
    array (
      'sum1' =&gt; '4914030',
      'count1' =&gt; '25',
      'sm__state_name__' =&gt; 'Cunane',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    10 =&gt; 
    array (
      'sum1' =&gt; '1167200',
      'count1' =&gt; '26',
      'sm__state_name__' =&gt; 'Cuanza North',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    11 =&gt; 
    array (
      'sum1' =&gt; '750080',
      'count1' =&gt; '10',
      'sm__state_name__' =&gt; 'Cuanza Sul',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    12 =&gt; 
    array (
      'sum1' =&gt; '2178100',
      'count1' =&gt; '6',
      'sm__state_name__' =&gt; 'Huambo',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    13 =&gt; 
    array (
      'sum1' =&gt; '1099934',
      'count1' =&gt; '25',
      'sm__state_name__' =&gt; 'Lunda North',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
    14 =&gt; 
    array (
      'sum1' =&gt; '410135',
      'count1' =&gt; '12',
      'sm__state_name__' =&gt; 'Malange',
      'om__order_date__year' =&gt; '2017',
      'om__order_date__quarter' =&gt; '3',
      'om__order_date__month' =&gt; '9',
    ),
  )
  </code></pre>
  <pre><code>Array
  (
      &quot;sm__state_name__&quot;,
      &quot;om__order_date__year&quot;,
      &quot;om__order_date__quarter&quot;,
      &quot;om__order_date__month&quot;,
  )
  </code></pre>
  <p><strong>ABOBE ARRAY INCLUDES CAN BE ANY NUMBER OF FIELDS</strong></p>
  <p>Below is sample output of What I want in return</p>
  <pre><code>{
      &quot;data&quot;: [
          {
              &quot;key&quot;: &quot;Luanda&quot;,
              &quot;items&quot;: [
                  {
                      &quot;key&quot;: 2017,
                      &quot;items&quot;: [
                          {
                              &quot;key&quot;: 3,
                              &quot;items&quot;: [
                                  {
                                      &quot;key&quot;: 8,
                                      &quot;items&quot;: null,
                                      &quot;count&quot;: 4,
                                      &quot;summary&quot;: [
                                          438380.9935
                                      ]
                                  },
                                  {
                                      &quot;key&quot;: 9,
                                      &quot;items&quot;: null,
                                      &quot;count&quot;: 1606,
                                      &quot;summary&quot;: [
                                          438380.9935
                                      ]
                                  },
                              ],
                              &quot;summary&quot;: [
                                  1285085.9636
                              ]
                          }
                      ],
                      &quot;summary&quot;: [
                          1285085.9636
                      ]
                  }
              ],
              &quot;summary&quot;: [
                  1285085.9636
              ]
          },
          {
              &quot;key&quot;: &quot;Benguela&quot;,
              &quot;items&quot;: [
                  {
                      &quot;key&quot;: 2017,
                      &quot;items&quot;: [
                          {
                              &quot;key&quot;: 3,
                              &quot;items&quot;: [
                                  {
                                      &quot;key&quot;: 9,
                                      &quot;items&quot;: null,
                                      &quot;count&quot;: 679,
                                      &quot;summary&quot;: [
                                          4781987.8575
                                      ]
                                  },
                              ],
                              &quot;summary&quot;: [
                                  15017212.0305
                              ]
                          }
                      ],
                      &quot;summary&quot;: [
                          15017212.0305
                      ]
                  }
              ],
              &quot;summary&quot;: [
                  15017212.0305
              ]
          },
          {...},
          {...},
          {...},
      ],
      &quot;totalCount&quot;: 22854
  }
  </code></pre>
  <p>Don't mind the summary value. I just put dummy values there.</p>
  <p>Is this kind of process even possible? Because I think of many different things, recursion, multiple loops, triple loop but couldn't think of way this could work.</p>
  <p>I know it's not an issue or bug. Sorry for that. But it would be great if someone could point me to right direction.</p>