# How to 3D Array to 1D Array -\> index conversion?

**URL:** <https://forums.unrealengine.com/t/how-to-3d-array-to-1d-array-index-conversion/349211>\
**Category:** Editor Scripting\
**Tags:** arrays, conversion, unreal-engine, question\
**Created:** [March 1, 2016, 3:59pm UTC](https://forums.unrealengine.com/t/how-to-3d-array-to-1d-array-index-conversion/349211 "2016-03-01T15:59:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![StreakyCat](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@StreakyCat](https://forums.unrealengine.com/u/StreakyCat)\
**Post date:** [March 1, 2016, 3:59pm UTC](https://forums.unrealengine.com/t/how-to-3d-array-to-1d-array-index-conversion/349211/1 "2016-03-01T15:59:19Z")

</div>

I need a conversion of 3D\<-\>1D index of an array[x][y][z] into array2[i].

I checked this thread at stackoverflow:

> <https://stackoverflow.com/questions/7367770/how-to-flatten-or-index-3d-array-in-1d-array>

and grabbed their solution for

```
i = x + y*xMax + z*yMax*xMax

```

But i stumbled uppon the following problem:  
Let’s assume i have a 3D “Wall” of the dimensions 1x3x3, which makes :

```
xMax = 1
yMax = 3
zMax = 3

```

Now storing those two elements at: [1][0][0] ; [0][1][0] inside array2[i] would result in:

```
[1][0][0] : i = 1 + 0*1 + 0*3*1 = 1
[0][1][0]: i = 0 + 1*1 + 0*3*1 = 1

```

Both elements interfere at the same index, what am i doing wrong or missing out?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_9918aa77](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_9918aa77](https://forums.unrealengine.com/u/anonymous_user_9918aa77)\
**Post date:** [September 22, 2018, 11:45pm UTC](https://forums.unrealengine.com/t/how-to-3d-array-to-1d-array-index-conversion/349211/2 "2018-09-22T23:45:20Z")

</div>

you shouldn’t call the variables xMax, yMax, and zMax, because they are not actually the maximum index number, but the number of indices.  
If you have an array A[4] it has indices of 0, 1, 2, and 3. the ‘max x’ is 3  
your 3d array of 1x3x3 is essentially a 2d array, since one of the dimensions is 1.The first point actually doesn’t exist in your wall.
