title: Refactor foreach with multilevel array
body: <p>I'm currently returning results from a sql statement in an array like so:</p>
  
  <pre><code>$results = [];
  
      foreach($promotionTool as $p){
          $results[] = $p;
      }
  
      return $results;
  </code></pre>
  
  <p>Which my console shows in an object with this structure:</p>
  
  <pre><code>(2) [{…}, {…}]
      0:
          codeID: "41"
          code: "123ABC"
          rule_type: "Category"
          attribute_type: "identifier"
          attribute_title: "category number"
          attribute_value: "234"
      1:
          codeID: "41"
          code: "123ABC"
          rule_type: "Category"
          attribute_type: "amount"
          attribute_title: "percent"
          attribute_value: "25"       
  </code></pre>
  
  <p>This is showing the data I expect but I'm a little bit lost on how to restructure this so that I can group on certain levels and finally return only an array of the attributes like so:</p>
  
  <pre><code>    codeID
          code
              rule_type
                  array(
                      0:
                          attribute_type: "identifier"
                          attribute_title: "category number"
                          attribute_value: "234"
                      1:
                          attribute_type: "amount"
                          attribute_title: "percent"
                          attribute_value: "25"   
                  )
  </code></pre>
  
  <p>How would I refactor my foreach to group at multiple levels in that way?</p>