Diego Vieira
Diego Vieira

Reputation: 1150

PHP Multidimensional in C#

I have been trying to figure this out, but couldn't. On php a multidimensional array would looks like this:

$array = array(
  1 => array("height" => 10, "width" => 20, "length" => 30),
  2 => array("height" => 10, "width" => 20, "length" => 30),
  3 => array("height" => 10, "width" => 20, "length" => 30),
);

And them I would get the value using

echo $array[3]["height"]; // output 10
echo $array[3]["width"]; // output 20

I have tried to use Dictionary, but I have no idea on how to accomplish this. Any help is appreciated Thanks

EDIT: This is the code I used that worked for me, hope this helps someone.

    Dictionary<int, Dictionary<string, int>> array = new Dictionary<int, Dictionary<string, int>>();
    Dictionary<string, int> sub = new Dictionary<string, int>();
    sub.Add("height", 10);
    sub.Add("width", 20);
    sub.Add("length", 30);
    array.Add(1, sub);

    sub = new Dictionary<string, int>();
    sub.Add("height", 10);
    sub.Add("width", 20);
    sub.Add("length", 30);
    array.Add(2, sub);

    sub = new Dictionary<string, int>();
    sub.Add("height", 10);
    sub.Add("width", 20);
    sub.Add("length", 30);
    array.Add(3, sub);

    Response.Write("height: " + array[1]["height"]);
    Response.Write("<br />width: " + array[1]["width"]);
    Response.Write("<br />length: " + array[1]["length"]);

Upvotes: 2

Views: 93

Answers (3)

digdig
digdig

Reputation: 240

What you are doing is a single-dimensional array of dictionaries:

Dictionary<string,Decimal>[] array =
{
    new Dictionary<string,Decimal>(){ 
        {"height", 10},
        {"width", 20},
        {"length", 30},
    },
    new Dictionary<string,Decimal>(){ 
        {"height", 10},
        {"width", 20},
        {"length", 30},
    },
    new Dictionary<string,Decimal>(){ 
        {"height", 10},
        {"width", 20},
        {"length", 30},
    },
};

Console.WriteLine(array[0]["height"]);

Output: 10

In C# you can declare IEnumrables with an Add method (of which both System.Array and Dictionary are children) explicitly with curly braces, so what we are doing here is declaring an array{} and three dictionaries{} inside it, and inside each dictionary three key/value pairs{}.

Upvotes: 2

Evan Levesque
Evan Levesque

Reputation: 3203

you can simply create your own class array container

class MyArray
{
public int height
{
    get;
    set;
}

public int width
{
    get;
    set;
}

public int length
{
    get;
    set;
}
}

var array = new MyArray[1];
array[0] = new MyArray() { height= 12, width = 32, length = 3 };

var list = new List<MyArray>();
list.Add(new MyArray());

var foo = new MyArray();
foo.height= 55;

var Y1= new MyArray() { height = 23 };

var X1 = new MyArray() { height  = 7 };
var X2 = new MyArray() { height  = 8 };
var list = new List<MyArray>() { X1, X2 };

Upvotes: 0

MatrixManAtYrService
MatrixManAtYrService

Reputation: 9131

Perhaps like this:

int[, ,] arrayName = new int[10, 20, 30];
int ten = arrayName.GetLength(0);
int twenty = arrayName.GetLength(1);
int thirty = arrayName.GetLength(2);

Individual values are then accessed like so:

arrayName[2,13,25] = 42;

Upvotes: 2

Related Questions