2016-09-22 62 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 }]}] 

]