2016-09-22 185 views
0

我有子阵列的阵列以下列格式组切片由子阵列值Golang

Array 
(
    [0] => Array 
     (
      [unit_id] => 6504 
      [assignment_name] => Grade assignment 
      [assignment_description] => 
      [assignment_total_score] => 10 
      [unit_type_name] => Homework 
      [is_graded] => 1 
      [standard_id] => 1219 
      [scoring_type] => score 
      [attempt_score] => 8 
      [unit_duedate] => 2016-02-10 09:00:00 
      [standard] => Array 
       (

        [0] => stdClass Object 
         (
          [unit_id] => 6504 
          [is_formal] => 1 
          [assignment_name] => Grade assignment 
          [assignment_description] => 
          [standard_id] => 1220 
          [standard_name] => 9-10.RL.3 
          [standard_description] => Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a 
         ) 

       ) 

     ) 

    [1] => Array 
     (
      [unit_id] => 8584 
      [assignment_name] => Sine and Cosecant Graphs 
      [assignment_description] => Define the sine and cosecant graphs using a unit circle 
      [assignment_total_score] => 15 
      [unit_type_name] => Paper 
      [scoring_type] => score 
      [attempt_score] => 0 
      [unit_duedate] => 2016-04-29 09:00:00 
      [standard] => Array 
       (

        [0] => stdClass Object 
         (
          [unit_id] => 8584 
          [is_formal] => 1 
          [assignment_name] => Sine and Cosecant Graphs 
          [assignment_description] => Define the sine and cosecant graphs using a unit circle 
          [assignment_total_score] => 15 
          [standard_id] => 82790 
          [standard_name] => 9-10.RL.7 


       ) 

     ) 

    [2] => Array 
     (
      [unit_id] => 11611 
      [assignment_name] => Adding 5 + 3 + 6 
      [assignment_description] => 
      [assignment_total_score] => 10 
      [unit_type_name] => Homework 
      [standard_id] => 82772 
      [scoring_type] => score 
      [attempt_score] => 0 
      [unit_duedate] => 2016-08-23 19:00:00 
      [standard] => Array 
       (
        [0] => stdClass Object 
         (
          [unit_id] => 11611 
          [is_formal] => 1 
          [assignment_name] => Adding 5 + 3 + 6 
          [assignment_description] => 
          [assignment_total_score] => 10 
          [standard_id] => 82772 
          [standard_name] => 9-10.RL.1 

         ) 

       ) 

     ) 

) 

我想基于在每个子阵列的unit_type_name字段它组成一个新的切片。

如何将切片按unit_type_name分组?有没有任何原生GO功能可以做到这一点? 如果我为循环上面那么我会得到一个重复的,我怎么能避免这一点?

+0

我是正确的,这是一个PHP数组? –

+0

是的,这是PHP的数组,但同样的数据是使用去朗读....如何根据unit_type_name进行分组? – Ali

+1

你是什么意思的“组片”?你想分类吗?你想把它们放在新的切片中吗?你有什么尝试,没有奏效? – JimB

回答

1

我不认为golang具有内置功能来帮助您做到这一点(我可能是错的)。我的保证是php数组将被转换为json对象。我设法得到下面的代码,以帮助你解决你的阵列(JSON格式)基于对unit_type_name 我创建了具有类似于数组键将

//StandardType ... 
type StandardType struct { 
    UnitID    int `json:"unit_id"` 
    IsFormal    int `json:"is_formal"` 
    AssignmentName  string `json:"assignment_name"` 
    AssignmentDescription string `json:"assignment_description"` 
    StandardID   int `json:"standard_id"` 
    StandardName   string `json:"standard_name"` 
    StandardDescription string `json:"standard_description"` 
} 

//AutoGenerated ... 
type AutoGenerated struct { 
    UnitID    int   `json:"unit_id"` 
    AssignmentName  string   `json:"assignment_name"` 
    AssignmentDescription string   `json:"assignment_description"` 
    AssignmentTotalScore int   `json:"assignment_total_score"` 
    UnitTypeName   string   `json:"unit_type_name"` 
    IsGraded    int   `json:"is_graded"` 
    StandardID   int   `json:"standard_id"` 
    ScoringType   string   `json:"scoring_type"` 
    AttemptScore   int   `json:"attempt_score"` 
    UnitDuedate   string   `json:"unit_duedate"` 
    Standard    []StandardType `json:"standard"` 
} 
var jsonData = `` 
func main() { 
    m := []AutoGenerated{} 
    err := json.Unmarshal([]byte(jsonData), &m) 
    if err != nil { 
     panic(err) 
    } 

我创建了一个JSON值两个结构地图保持unit_type_name

sliceKeys := make(map[string]string) 

我还创建映射到认为具有相似的unit_type_name键在自动生成的阵列的阵列

groupedSlices := make(map[string][]AutoGenerated) 

然后,我通过解码JSON字符串循环搜索的unit_type_name

for i := range m { 

如果unit_type_name在我的阵列项目添加到组片的关键切片已经存在

 if _, ok := sliceKeys[m[i].UnitTypeName]; ok { 
     autogenerated := groupedSlices[m[i].UnitTypeName] 
     autogenerated = append(autogenerated, m[i]) 
     groupedSlices[m[i].UnitTypeName] = autogenerated 
     } else { 

否则我创建了一个新的数组键和项目添加到它

 sliceKeys[m[i].UnitTypeName] = m[i].UnitTypeName 
     autogenerated := []AutoGenerated{} 
     autogenerated = append(autogenerated, m[i]) 
     groupedSlices[m[i].UnitTypeName] = autogenerated 
    } 
    } 
    fmt.Println(sliceKeys) 
    fmt.Println(groupedSlices) 
} 

输入:

[{"unit_id": 6504,"assignment_name": "Grade assignment","assignment_description": "","assignment_total_score": 10,"unit_type_name": "Homework","is_graded": 1,"standard_id": 1219, 
"scoring_type": "score","attempt_score": 8,"unit_duedate": "2016-02-10 09:00:00", 
"standard": [{"unit_id": 6504,"is_formal": 1,"assignment_name": "Grade assignment","assignment_description": "", 
"standard_id": 1220,"standard_name": "9-10.RL.3","standard_description": "Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a " 
}]},{"unit_id": 6504,"assignment_name": "Grade assignment","assignment_description": "","assignment_total_score": 10, 
"unit_type_name": "Paper","is_graded": 1,"standard_id": 1219,"scoring_type": "score","attempt_score": 8,"unit_duedate": "2016-02-10 09:00:00","standard": [{"unit_id": 6504,"is_formal": 1,"assignment_name": "Grade assignment","assignment_description": "","standard_id": 1220,"standard_name": "9-10.RL.3","standard_description": "Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a "}]},{ 
"unit_id": 6504,"assignment_name": "Grade assignment","assignment_description": "", 
"assignment_total_score": 10,"unit_type_name": "Aything else","is_graded": 1,"standard_id": 1219, 
"scoring_type": "score","attempt_score": 8,"unit_duedate": "2016-02-10 09:00:00","standard": [{ 
"unit_id": 6504,"is_formal": 1,"assignment_name": "Grade assignment","assignment_description": "","standard_id": 1220, 
"standard_name": "9-10.RL.3","standard_description": "Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a "}]}] 

输出:

map[Homework:Homework Paper:Paper Aything else:Aything else] 

map[ 
Homework:[ 
{6504 Grade assignment 10 Homework 1 1219 score 8 2016-02-10 09:00:00 [{6504 1 Grade assignment 1220 9-10.RL.3 Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a }]} 
] 

Paper:[ 
{6504 Grade assignment 10 Paper 1 1219 score 8 2016-02-10 09:00:00 [{6504 1 Grade assignment 1220 9-10.RL.3 Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a }]} 
] 

Aything else:[ 
{6504 Grade assignment 10 Aything else 1 1219 score 8 2016-02-10 09:00:00 [{6504 1 Grade assignment 1220 9-10.RL.3 Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a }]}] 

]