I want to store ids in an array and attach them to an address. For example: There are 0 houses. User1 buys an house so id 0 gets added to the array userHouses[msg.sender]. User2 buys 2 houses so it 1 and 2 gets added to userHouses[msg.sender]. user1 buys another house, id 3 gets added to userHouses[msg.sender] and so on. I could do

mapping(uint => address) houseOwner;


houseOwner[0] = msg.sender; // User1
houseOwner[1] = msg.sender; // User2
houseOwner[2] = msg.sender; // User2
houseOwner[3] = msg.sender; // User1

But when there are 1.000.000 houses of 10.000 users it gets too much. So I want instead 10.000 arrays for each address and simply add the houses into that address (like houses[msg.address]). Also it's easier and cheaper to loop only houses of the user instead of looping all houses and check if the house owner is the user.

I know how to make an array (uint[] houses) but I don't know how to make arrays for each address (uint[] houses[msg.sender]?).

2 Answers 2


If I understand correctly, I believe you want this:

mapping (address => uint[]) houses;

Then you can do things like this:

  • Sorry for the late answer! Yes, great, that's it. I found it already before, but forgot to answer.
    – dkb
    Commented Mar 6, 2018 at 15:17

pseudo code update v2

pragma solidity ^0.4.18;
contract Houses {
    struct House {
        address owner;

    House[] public houses;

    function buyHouse() public payable {
        House memory tempHouse = House(msg.sender);

    function getTotalHomesSold() public view returns (uint256 count) {
        return houses.length;
  • Thanks, but that did not fix my problem. I wanted an array for each user (like house[user][houseId]) but your example only creates a house array what exactly is what I did not want :). So to clarify, imagine following: You have 10.000 houses owned by 2.000 people. You could now create an house array and add those 10.000 houses (your example), so the array has 10.000 values. But what I wanted is, to create arrays with less values. In example it would be 2.000 different arrays but with each the biggest amount of values would be for example 100 (if an user has 100 houses). Thank you anyway! :)
    – dkb
    Commented Mar 6, 2018 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.