我有查询结果:
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?