-2

我有查询结果:

array (
  [0]=>array (
    "id"=>"3"
    "role_id"=>  "62"
    "user_id"=> "20"
    "service_id"=> "1"
    "field_id"=> "4"
    "value"=> "asdfgh"
  )
  [1]=>array (
    "id"=> "4"
    "role_id"=> "62"
    "user_id"=> "20"
    "service_id"=> "2"
    "field_id"=> "3"
    "value"=> "qwerty"
  )
  [2]=>array (
    "id"=> "5"
    "role_id"=> "62"
    "user_id"=> "20"
    "service_id"=> "2"
    "field_id"=>  "4"
    "value"=>  "asdfgh"
  )
  [3]=>array (
    "id"=> "6"
    "role_id"=> "62"
    "user_id"=> "20"
    "service_id"=> "2"
    "field_id"=> "16"
    "value"=>  "zxcvbn"
  )
)

我想这样安排这些数据:

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

结果首先按 role_id 排序,然后按 service_id 排序。

我想显示表格,其中每一行都是一个单独的角色,在行中我想显示按服务分组的字段。

怎么做?

我被困在这里:

$roles = array();
foreach($resultArray as $ra){
    $roles[$ra['role_id']] = array(
       ...
    )
}

现在我在想...

$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?
4

2 回答 2

4

干得好;

PhpFiddle

<?php
$initial = array (
  0=>array (
    "id"=>"3",
    "role_id"=>  "62",
    "user_id"=> "20",
    "service_id"=> "1",
    "field_id"=> "4",
    "value"=> "asdfgh"
  ),
  1=>array (
    "id"=> "4",
    "role_id"=> "62",
    "user_id"=> "20",
    "service_id"=> "2",
    "field_id"=> "3",
    "value"=> "qwerty"
  ),
  2=>array (
    "id"=> "5",
    "role_id"=> "62",
    "user_id"=> "20",
    "service_id"=> "2",
    "field_id"=>  "4",
    "value"=>  "asdfgh"
  ),
  3 =>array (
    "id"=> "6",
    "role_id"=> "62",
    "user_id"=> "20",
    "service_id"=> "2",
    "field_id"=> "16",
    "value"=>  "zxcvbn"
  )
);

$roles = array(
    'services' => array()
);

foreach($initial as $key => $value) {
    if (!isset($roles['services'][$value['service_id']]['fields'])) {
        $roles['services'][$value['service_id']]['fields'] = array();
    }

    $roles['services'][$value['service_id']]['fields'][] = array(
        'id' => $value['field_id'],
        'value' => $value['value']
    );
}

echo '<pre>';
print_r($roles);
echo '</pre>';
?>

结果

Array
(
    [services] => Array
        (
            [1] => Array
                (
                    [fields] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 4
                                    [value] => asdfgh
                                )

                        )

                )

            [2] => Array
                (
                    [fields] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 3
                                    [value] => qwerty
                                )

                            [1] => Array
                                (
                                    [id] => 4
                                    [value] => asdfgh
                                )

                            [2] => Array
                                (
                                    [id] => 16
                                    [value] => zxcvbn
                                )

                        )

                )

        )

)
于 2013-04-26T22:26:11.880 回答
1

phpfiddle

    $services = array();

    foreach ($resultArray as $v) {
       if( ! isset($services[$v['service_id']] )) {  
          $services[$v['service_id']] = array('fields' => array()); 
       } 
       $services[$v['service_id']]['fields'][] = array( 'id' => $v['field_id'], 'value' => $v['value']);
    }

    $roles = array('services' => $services);
于 2013-04-26T22:28:24.137 回答