2012-11-20 90 views
1

我一直在试图找到这个awnser一段时间。我正在将一个网站从php转换为asp。我有一个这样的数组。asp中的多维关联数组

$students = array(

    array(
     "id" => "1", 
     "name" => "John", 
     "group" => "A" 
    ), 
    array(
     "id" => "2", 
     "name" => "Joe", 
     "group" => "A" 
    ), 
    array(
     "id" => "3", 
     "name" => "Derp", 
     "group" => "B" 
    ), 
); 

foreach($student as $slacker){ 
    //use the data 
} 

是否有任何替代方案,甚至接近这与asp?

+0

你想把它转换成asp.net数据结构吗? – Adil

+0

当然,任何能让我以类似方式使用它的东西。 – user1738750

+0

在c#数组中与php不同。数组是索引和值的组合。该索引是一个数字字段。它不像PHP,你可以在索引中有任何东西。在您的要求中,您可以使用结构或类集合或数组。 –

回答

2

您可以创建一个类并使用泛型列表来保存类类型的数组。

public class YourGroup 
{ 
    public string id { get; set; }; 
    public string name { get; set; }; 
    public string group { get; set; };  
} 

List<YourGroup> lstGroup = new List<YourGroup>(); 
lstGroup.Add(new YourGroup(id ="1", name="Jon", group="A1")); 
lstGroup.Add(new YourGroup(id ="2", name="Jon", group="A2")); 
lstGroup.Add(new YourGroup(id ="3", name="Jon", group="A3")); 
string idOfFirst lstGroup[0].id; 
0

您可能正在寻找所谓的词典。虽然它不是很深嵌套像$ myarray的[“富”] [“酒吧”]字典将允许你这样的东西

Dictionary<int, MyObject> myDictionary = new Dictionary<int, MyObject>(); 

在您的MyObject持有下列对象

class MyObject 
{ 
     public string name; 
     public string group; 
} 

为结果可以遍历它是这样

MyObject foo = new MyObject(); 

foo.name = "tada!"; 
foo.group = "foo"; 

myDictionary.add(1, foo); 

foreach (MyObject obj in Dictionary.Values) 
{ 
    //Do Stuff 
} 

完全串相关联阵列将是

Dictionary<string, string> myDictionary = new Dictionary<string, string>(); 

string something = myDictionary["value"];