I have to flatted a 3d array in order to be serialized.
Let's start with this:

```
int[,,] array3D = new int[,,] { 
            { { 1, 2 }, { 3, 4 }, {5,6 }, {7,8 } },
            { { 9, 10}, { 11, 12},{ 13,14} , {15,16 }},
            { { 17, 18}, { 19, 20},{ 21,22}, {23,24 } }
        };

```

which makes it like this (something like 1,2,3,4,...,24):

[![enter image description here](https://i.stack.imgur.com/uMTGW.png)](https://i.stack.imgur.com/uMTGW.png)

So now I have this s/r 

```
public static T[] Flatten<T>(T[,,] arr)
{
    int rows0 = arr.GetLength(0);
    int rows1 = arr.GetLength(1);
    int rows2 = arr.GetLength(2);
    T[] arrFlattened = new T[rows0 * rows1* rows2];

    int i, j, k;
    for (k = 0; k < rows2; k++)
    {
        for (j = 0; j < rows1; j++)
        {
            for (i = 0; i < rows0; i++)
            {
                var test = arr[i, j, k];
                int index = i + j * rows0 + k * rows1;
                arrFlattened[index] = test;
            }
        }
    }
    return arrFlattened;
}

```

which flattens the 3d matrix into a 1d array

[![enter image description here](https://i.stack.imgur.com/v1aUN.png)](https://i.stack.imgur.com/v1aUN.png)

I am not smart enough to understand if the procedure is correct but let's go further.
I then Expand with the following s/r

```
public static T[,,] Expand<T>(T[] arr, int rows0, int rows1)
{
    int length = arr.GetLength(0);
    int rows2 = length / rows0 / rows1;

    T[,,] arrExpanded = new T[rows0, rows1, rows2];
    for (int k = 0; k < rows2; k++)
    {
        for (int j = 0; j < rows1; j++)
        {
            for (int i = 0; i < rows0; i++)
            {
                T test = arr[i + j * rows0 + k * rows1];
                arrExpanded[i, j, k] = test;
            }
        }
    }
    return arrExpanded;
}

```

but the result is the following:

[![enter image description here](https://i.stack.imgur.com/elEzN.png)](https://i.stack.imgur.com/elEzN.png)

So nothing like 1,2,3,4,5....24
I know that the error might be a trivial one but try as I might I can't find it.
Thank in advance.

Patrick

---

Thanks for helping all 3 solution were amazing and working but I chose the one more easily to understand and debug for me