I am learning solidity and my test project is a simple ticket system.

Basically, users will use the smart contract to buy movie tickets. The contract will issue a ticket and store the user's address and the number of tickets purchased.

I want to store this in a type of array like so:

tickets[ShowIdentity][] = [

I have read that mappings are the best way to store data however I am unsure how to go about using it.

Any advice and/or some sample code would be greatly appreciated.

1 Answer 1


I have added some extra meta-data to this example, but the following shows how you can use a combination of structures, arrays, and mappings to create rich data objects for each Ticket and Show (including both individual user data and aggregate show data). Hope it helps!

    struct Ticket {
       uint seatNumber;                       // the assigned seat number for this ticket
       uint purchaseTime;                     // the timestamp when this ticket was purchased

    struct Show {
       uint showtime;                        // the timestamp of the show
       uint ticketPrice;                     // the price per ticket of the show
       uint totalRevenue;                    // the total amount of funds gathered from ticket sales in a show
       uint totalSeats;                      // the initial number of seats in this show
       uint seatsSold;                       // the total number of seats sold for this show
       mapping (address => Ticket[]) tickets;       // an array of Tickets assigned to each address(user) in each show

    mapping (uint => Show) public shows;   // map show ID number to Show information

Notice that an array of Tickets is mapped to each individual user, this is probably the only place an array should be used since the number of seats will stay relatively small. Usually arrays should be avoided for large datasets (things like each individual user, or each show over many years of shows). Arrays often need to be iterated over, and this can cost a lot of gas!

  • This is a really nice example, thank you. I have used it as a template in my test app.
    – HappyCoder
    Sep 4, 2017 at 11:36
  • No to access the data, would it be correct to do something like this: Show storage showObject = shows[showIdentity];
    – HappyCoder
    Sep 4, 2017 at 11:38
  • 1
    Yes, that is correct. And to access lower level information, here an example of the "deepest" information. showObject.tickets[useraddress][0].seatNumber. This would return the first ticket seatNumber for a user in the show object. (Also I forgot to name the Ticket mapping and have edited my answer to reflect this) Glad to help :)
    – ZeroSum
    Sep 4, 2017 at 23:48

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.