views:

90

answers:

2

Hey guys, If I've got this data structure in PHP, what data structure is the best for it in C#?

$array = Array(
  [dummy1] => Array (
                [0] => "dffas",
                [1] => "asdas2",
                [2] => "asdasda"
              ),
  [dummy2] => Array (
                [0] => "asdasd",
                [1] => "sdfsdfs",
                [2] => "sdfsdf"
              )
)

And so on. But I need to be able to add data to the nested array on the fly, something like this:

$array["dummy1"][] = "bopnf";

I tried using a Hashtable, but when I go hashtable.add(key,value), where key is my dummy1, and value is one of the array elements, it will throw an error stating that an element already contains that key.

So I'm thinking a hashtable is not the correct way of tackling this.

Thanks, Psy

+2  A: 

For example you could use a Dictionary<string, List<string>>. So you could do:

var dict=new Dictionary<string, List<string>>();
dict.add("dummy1", new List<string>() {"dffas", "asdas2", "asdasda"});
dict.add("dummy2", //etc...);

And to add data:

dict["dummy1"].Add("bopnf");
Konamiman
I literally just tried out this combination and was about to add that it worked, you beat me to it.If anyone knows any better ways then please do post :)
Psytronic
A: 

the reason your getting the error with the hashtable is because each key needs to be unique so this is probably not the best approach, the use of a dictionary as Konamiman explain would be best

harryovers