How to change results to arrays?
<p>I have result from query:</p>

<pre><code>array (
  [0]=&gt;array (
    "id"=&gt;"3"
    "role_id"=&gt;  "62"
    "user_id"=&gt; "20"
    "service_id"=&gt; "1"
    "field_id"=&gt; "4"
    "value"=&gt; "asdfgh"
  )
  [1]=&gt;array (
    "id"=&gt; "4"
    "role_id"=&gt; "62"
    "user_id"=&gt; "20"
    "service_id"=&gt; "2"
    "field_id"=&gt; "3"
    "value"=&gt; "qwerty"
  )
  [2]=&gt;array (
    "id"=&gt; "5"
    "role_id"=&gt; "62"
    "user_id"=&gt; "20"
    "service_id"=&gt; "2"
    "field_id"=&gt;  "4"
    "value"=&gt;  "asdfgh"
  )
  [3]=&gt;array (
    "id"=&gt; "6"
    "role_id"=&gt; "62"
    "user_id"=&gt; "20"
    "service_id"=&gt; "2"
    "field_id"=&gt; "16"
    "value"=&gt;  "zxcvbn"
  )
)
</code></pre>

<p>And I want to arrange this data like this:</p>

<pre><code>$roles = array(
    'services' =&gt; array(
         [2] =&gt; array( // this [2] is service_id
             'fields'=&gt;array(
                 [0]=&gt;array(
                     "id"=&gt;"16", "value"=&gt;"zxcvbn"
                 )
                 [1]=&gt;array(
                     "id"=&gt;"4", "value"=&gt;"asdfgh"
                 )
                 [2]=&gt;array(
                     "id"=&gt;"3", "value"=&gt;"qwerty"
                 )
              )
         [1] =&gt; array( // this [1] is service_id
             'fields'=&gt;array(
                 [0]=&gt;array(
                     "id"=&gt;"4", "value"=&gt;"asdfgh"
                 )
              )
          )
      )
 )
</code></pre>

<p>Result is sorted first by role_id then by service_id.</p>

<p>I want to show table where each row is a separate role, in row I want to show fields grouped by service.</p>

<p>How to do it?</p>

<p>I'm stuck here:</p>

<pre><code>$roles = array();
foreach($resultArray as $ra){
    $roles[$ra['role_id']] = array(
       ...
    )
}
</code></pre>

<p>Now I'm thinking...</p>

<pre><code>$temp_role = null;
$roles = array();
foreach($resultArray as $ra){
    if($ra['role_id']!=$temp_role){
        $temp_role = $ra['role_id']
        ... something like this, but is it a good idea?
</code></pre>
