# Create a two-dimensional array in Solidity

How to create arrays addresses using solidity? And we can write two-dimensional array?

``````bool[2][] m_pairsOfFlags;
``````

Here is an example of the array, but it does not work

• This might help mapping (uint256 => mapping (uint256 => bool)) m_pairsOfFlags; Commented Feb 2, 2017 at 11:58
• the example you are talking about is in the official documentation : solidity.readthedocs.io/en/develop/types.html So what do you exactly mean by does not work? Commented Feb 2, 2017 at 14:03

Yes you can.

This little snippet might help. It's just a little toy to show how the two-dimensional array elements are referenced.

It might be helpful to point out that things may be a little counter-intuitive because the X & Y axis may seem to be reversed.

``````bool[2][] flags;
``````

It starts by describing a length 2 array of bools. Then it goes on to say there will be a dynamic list of those called `flags`.

So, to reference such things, the dynamic dimension is high order.

``````bool flag = flags[dynamicIndex][lengthTwoIndex];
``````

The above might look backwards, so that can be disorienting.

Some functions I put together to demo this in Remix:

``````pragma solidity ^0.4.6;

// set all the inserts to true for simplicity

contract Arrays{

// dynamic list of length 2 bools
bool[2][] flags;

function Arrays() {
// append a length 2 array to the dynamic list of flags
flags.push([true,true]);
}

function appendFlag() returns(uint length) {
// append another length 2 array to the dynamic dimension
return flags.push([true,true]);
}

// return a length 2 array stored in the dynamic dimension
// will throw if index > flags.length-1 (index starts at 0)
function getFlags(uint index) constant returns(bool[2] flagList) {
return(flags[index]);
}

// return one flag from the array
function getFlag(uint dynamicIndex, uint lengthTwoIndex) constant returns(bool flag) {
return flags[dynamicIndex][lengthTwoIndex];
}

// return a count of length 2 arrays stored in flags;
function getFlagsCount() constant returns(uint count) {
return flags.length;
}

}
``````

Hope it helps.

• Was it a design choice of solidity to reverse the indexes or is it a bug that will be fixed later (or never cuz we're stuck with it)? Commented May 6, 2019 at 20:54
• It's a design choice. Commented May 6, 2019 at 21:50