asp中的多维关联数组

时间:2012-11-20 15:58:03

标签: asp.net arrays

我一直试图找到这个的芒果。我正在将一个站点从php转换为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接近?

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

您可能正在寻找所谓的词典。虽然它的嵌套效果不像$ myArray ['foo'] ['bar']但字典会让你像

一样
 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"];