0

I have an associative array in PHP

$data=array(
    100=>array(
        "first"=>array(4,24,5,0),
        "second"=>array(42,58,23,8),
        "third"=>array(4,1,14,0),
        "fourth"=>array(49,41,28,2),
        "fifth"=>array(41,30,13,5)
    ),
    500=>array(
        "first"=>array(4,24,5,0),
        "second"=>array(42,58,23,8),
        "third"=>array(4,1,14,0),
        "fourth"=>array(49,41,28,2),
        "fifth"=>array(41,30,13,5)
    )
);



and i want to move that code into c# programming, but i don't know how to move it. I try to use List but i just can take in 100 and 500 only.
How to move it into c# programming?

R.Mahtub
  • 1
  • 3
  • You have made right choice by using `List`, what is the problem in the implementation which you encountered? – Ian Jan 04 '16 at 16:41
  • Please show what you actually tried in `c#` – Jon Jan 04 '16 at 16:42
  • 1
    @Ian A `List` is not an associative array, so what makes you think that it's appropriate at all? – Servy Jan 04 '16 at 16:42
  • @Servy I should rather say, `List` is okay, I think... perhaps `right` is quite strong word (could be for lacking of better understanding). But my reasoning is because the data set is small, `List` is easy to process, and you can put KVP in the `List` if be needed as well. Perhaps there is better/more efficient choice which is beyond my knowledge. But for the given case, I would say `List` is acceptable. – Ian Jan 04 '16 at 16:47
  • Consider creating classes that represent the concepts that you are trying to model using the arrays. – Yacoub Massad Jan 04 '16 at 17:00

6 Answers6

3

Use the Dictionary class.

So you can do something like this:

        var dict = new Dictionary<int, Dictionary<string, int[]>>
        {
            {
                100, new Dictionary<string, int[]>
                {
                    {"first", new[] {4, 24, 5, 0}},
                    {"second", new[] {42, 58, 23, 8}}
                }
            },
            {
                200, new Dictionary<string, int[]>
                {
                    {"first", new[] {4, 24, 5, 0}},
                    {"second", new[] {42, 58, 23, 8}}
                }
            }
        };
Martino Bordin
  • 1,412
  • 1
  • 14
  • 29
3

Lists and Dictionaries are the way to go.

First you should make sure you are using the Generic collections namespace:

using System.Collections.Generic;

At the top of your file.

you can then create your Dictionary of Dictionary of List of integer like so:

var data = new Dictionary<int,Dictionary<string,List<int>>>() {
   {100, new Dictionary<string, List<int>>() {
      {"first", new List<int>() {4, 24, 5, 0}},
      {"second", new List<int>() {42, 58, 23, 8}} //TODO - add third, fourth etc.
   }},
   {500, new Dictionary<string, List<int>>() {
      {"first", new List<int>() {4, 24, 5, 0}},
      {"second", new List<int>() {42, 58, 23, 8}} //TODO - add third, fourth etc.
   }}
}
Lew
  • 1,248
  • 8
  • 13
2
var data = new Dictionary<int, Dictionary<string, List<int>>>();

associative array are known as Dictionary in c#

As a sidenote: This feels very unintuitive in C#: Wrapping multiple Lists/Dictionarys inside each other isn't the most elegant solution.


Depending on your requirements it might be a better approach to wrap this construct inside classes:

Could be something like this:

public class DataContainer {
  public int Index { get; set; }
  public DataValue MyValue { get; set; }
}

public class DataValue {
  public string Name { get; set; }
  public List<int> IntegerValues { get; set; }
}

Then you could simply have a List of DataContainers: var data = new List<DataContainer>();

jHilscher
  • 1,810
  • 2
  • 25
  • 29
2

Associative arrays of PHP map to the Dictionary class of C#. Numeric arrays map to the List class, or to the more straightforward array (e.g. int[], string[]).

One major difference between C# and PHP is the fact that C# is a strongly typed programming language, meaning you need to declare the type of the variable. You can then use the literal syntax to build the dictionary.

var data = new Dictionary<int, Dictionary<string, int[] > >()
{
    [100] = new Dictionary<string, int[] >() 
    {
        ["first"] = new int[] {4, 24, 5, 0},
        ["second"] = new int[] {42, 58, 23, 8}
        // the rest of the items
    }
    // the rest of the items
};

Note. Thanks @juharr for pointing out, the above syntax works from C# 6. For previous versions, a similar syntax can be used:

var data = new Dictionary<int, Dictionary<string, int[] > >()
{
    { 100, new Dictionary<string, int[] >()
        {
            {"first", new int[] {4, 24, 5, 0} },
            {"second", new int[] {42, 58, 23, 8} }
            // the rest of the items
        }
    }
    // the rest of the items
};
Cristik
  • 30,989
  • 25
  • 91
  • 127
1

I think you're looking for a Dictionary: https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx

This is my 1 to 1 mapping of what I think you posted (you can replace List by int[] as well):

Note: I do see that your inner dictionaries are always identical which there's no point in having 2 inner dictionaries but like I said, it's a 1 to 1 mapping of what I see

            var mainDictionary = new Dictionary<int, Dictionary<string, List<int>>>();
            var firstInnerDictionary = new Dictionary<string, List<int>>();
            var secondInnerDictionary = new Dictionary<string, List<int>>();

            firstInnerDictionary.Add("first", new List<int>{4,24,5,0});
            mainDictionary.Add(100, firstInnerDictionary);

            secondInnerDictionary.Add("first", new List<int>{4,24,5,0});
            mainDictionary.Add(500, firstInnerDictionary);
Starceaker
  • 631
  • 2
  • 5
  • 14
0

My suggestion would be to use a Dictionary as this:

var data = new Dictionary<int, Dictionary<string, List<int>>>;
data[100] =new Dictionary<string, List<int>>();
data[100]["first"] = new List<int>();
data[100]["first"].AddRange(new [] {4, 24, 5, 0});
// etc...

There are dictionary and list initialisers to make the code more compact though.

René Vogt
  • 43,056
  • 14
  • 77
  • 99