2014-10-28 3 views
0

나는 누군가가 이것을 어떻게하는지 생각해주기를 바랍니다.루프 루프의 논리, 고유 ID

0-> id=1 other_ids = 2,3,4 
1-> id=2 other_ids = 7,5 

I :

0 -> id=1 other_id = 2 
1 -> id=1 other_id = 3 
2 -> id=1 other_id = 4 
3 -> id=2 other_id = 7 
4 -> id=2 other_id = 5 

이제 내 루프에서, 나는 결과가 될 수 있도록 다른 ID 데이터를 고유 한 ID를 밀어 연결하는 싶습니다

나는이 데이터를 임시 변수를 사용하려고 생각했지만 데이터 구조를 사용할 수 없습니다.

그런데 codeigniter를 사용하고 있습니다. 결과는 모델에서 나왔고 PHP를 통해 컨트롤러에서 조작하고 싶습니다. 클래스를

public class Person 
    { 
     public int id { get; set; } 
     public int other_id { get; set; } 
    } 

만들기 1 단계 : 나는 희망

답변

0

1 단계 데 도움이 코드가

string output = string.Empty; 

     List<Person> lst = new List<Person>(); 
     lst.Add(new Person(){ id= 1, other_id = 2}); 
     lst.Add(new Person(){ id= 1, other_id = 3}); 
     lst.Add(new Person(){ id= 1, other_id = 4}); 
     lst.Add(new Person(){ id= 2, other_id = 7}); 
     lst.Add(new Person(){ id= 2, other_id =5 }); 

     List<int> idlst = new List<int>(); 
     idlst = lst.Select(x => x.id).Distinct().ToList(); 


     foreach (int _id in idlst) 
     { 
      List<int> other_idlist = lst.Where(x => x.id == _id).Select(x=> x.other_id).ToList(); 
      output += string.Format("id={0} other_ids = {1}", _id, string.Join(",", other_idlist)) + Environment.NewLine; 
     } 

     MessageBox.Show(output); 
+0

는 귀하의 의견 주셔서 감사합니다 도움이 될 것입니다. PHP에 대한 질문에 태그를 추가하지 않았습니다. 내가 주어진 코드를 검사하고 PHP로 변환 할 수 있는지를 알 수 있습니다. – derping