Here is how three programming languages handle a common problem: how do you organize and keep track of useful data?

Arrays solve a somewhat complicated, and very interesting, problem in software programming. They're called arrays in PHP, libraries in Python, and tables in Lua. However, while the name differs, the problem they solve is the same.

Imagine you’re playing a video game with weapons, or food if you’re non-violent and enjoy games like Animal Crossing or Cooking Mama. How do you keep track of each object available in your game? And, for each object, how do you keep track of its properties? For example, if the video game has weapons, how do you keep track of any photos of the weapon that appear onscreen?

This is the problem arrays, libraries, and tables (and whatever else you might call them) are designed to solve. Arrays provide a predictable structure to make data easy to retrieve from computer memory.

Let’s get back to our game example. Let’s say there are five possible weapons in this game:

  • Rock
  • Paper
  • Scissors
  • Axe
  • Scimitar

These are all weapons so let’s assume we create a data structure called Weapons. This structure is a large imaginary box where we can store weapons and their properties.

Therefore, referring to the rock as Weapons(Rock) will allow the game to find the rock when you click on its image onscreen. The game would find the data structure called “Weapons” then look inside it to find the “Rock” weapon.

Each Weapons(Rock) itself is a small data structure nested inside the Weapons data structure. Each of these smaller structures might include properties, for example, the file path to a small image of the weapon and the power level for the weapon.

To retrieve the image of the Axe, for example, you might refer to Weapons(Axe(Photo)). Or the power level of the scimitar might be Weapons(Scimitar(Power)). In each case, the game would travel down inside the nested structures. With Weapons(Scimitar(Power)), first the game would find the Weapons data structure, then Scimitar inside of Weapons, then the Power setting for Scimitar inside the Scimitar data structure.

"Weapons(Scimitar(Power))" also is an example how arrays reference their data:

  • Weapons is the name of the array (or library or table).
  • Scimitar is the key used to identify a set of data within the array.
  • Power is data stored at the location within the array.

Nesting collections of data, one inside the other, makes it easy to store and retrieve data. In some programming languages, it also is possible to store arrays within arrays. So our five types of weapons might each contain an array of properties, like photos and power levels.

Because we’ve made it easy to store and find data about our game weapons, we can do several more things. Let’s say two players pick a different weapon then start smashing each other. Which weapon is more powerful?

The pseudo code might look like this, where you have the axe and another play has the scimitar:

  • if (Weapons(Scimitar(Power)) is greater than (Weapons(Axe(Power)) you lose.
  • if (Weapons(Scimitar(Power)) is less than (Weapons(Axe(Power)) you win.

As you can see, arrays are very useful ways to structure and store lots of data to make it easy to recover then use the data.

Here are a few details about how arrays are used in different programming languages. These examples only highlight the essential nature of array structures. They are not detailed production ready code.


weapons = { } -- create the table to store data
weapons[rock] = 2 -- in this crude example, set rock's power to 2
weapons[paper] = 0 -- set paper power to 0
weapons[scissors] = 1 -- set scissors power to 1
weapons[axe] = 3 -- set axe power to 3
weapons[scimitar] = 4 -- set scimitar power to 4
print(weapons[axe]) -- retrieves power setting for the axe

In this example, to avoid complexity, we only create the table then use the names of the weapons as keys to get a value. We say the value is the power level of each weapon. In the real world, each weapon key would add a table with all the properties for the weapon. This example also shows how to build a table line by line. As you’ll see with PHP and Python, Lua tables also can be built as a single long line of code.

This code also uses print() which would display the results onscreen. Actual live code would, instead, assign the value of weapons[axe] to a variable so you could compare the power levels of different weapons.


$arrWeapons = array[
        "rock" => 2,
        "paper" => 0,
        "scissors" => 1,
        "axe" => 3,
        "scimitar" => 4,


As with Lua, while PHP has the ability to put arrays inside of arrays, this example only assigns one value to each key. Also note the use of a single set of square brackets to contain the full list of each key and its assigned value. And each value is assigned with the => symbol. Finally, the var_dump function in PHP would output the full list of keys and values for this array.


weapons = { 'rock' : 2, 'paper' : 0, 'scissors' : 1, 'axe' : 3, 'scimitar' : 4 }

Python allows you to create a dictionary as a single list. Assignment of a value to a key is done with the colon (:) character placed between the key and its value. The second line prints the value of the Rock key to a screen if you use a command prompt to work with Python.

Learn More

Lua Tables

PHP Arrays

Python Dictionaries

Arrays (in Computer Science)


  • Tim Slavin

    Tim is an award-winning writer and technologist who enjoys teaching tech to non-technical people. He has many years experience with web sites and applications in business, technical, and creative roles. He and his wife have two kids, now teenagers, who are mad about video games.

Also In The October 2013 Issue

An Interview with Troy Hunt

Troy Hunt is a software architect and Microsoft Most Valued Professional (MVP) focusing on security concepts and process improvement in a Fortune 50 company. He's based in Australia.

1Password, LastPass, RoboForm

If you use a password you created that is less than eight characters, your password is vulnerable to hacking. Here are three ways to create and use secure passwords online.

How to Write Secure Code

Coding securely doesn't have to kill the joy of programming. In fact, learning how to code securely provides insights into languages and computing.

How to Code HTML Email

How to code an HTML email like the ones you open every day turns out to be an offbeat software coding challenge.

What is an SSL Certificate?

How to tell if a web page is secure is one of the most basic yet least obvious ways to protect your data online.

Where to Find Command Line Interface Software

One key computing skill is the ability to use command line interface (CLI) software to enter commands to control a computer. Here are some options.


Lua is a comparatively simple programming language used in a wide range of places, from digital TVs to video games to phone applications. It's also designed to be simple to use and lightweight.


Here is how three programming languages handle a common problem: how do you organize and keep track of useful data?

Linux Command List for Command Line Interfaces

Some of the most common commands you'll need for a command line interface (CLI), in a Linux command list.

Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter.

News Wire Stories for October 2013

Must read stories about computer science, software programming, and technology for September 2013.

Learn More Links for October 2013

Links from the bottom of all the October 2013 articles, collected in one place for you to print, share, or bookmark.

Interested but not ready to subscribe? Sign-up for our free monthly email newsletter with curated site content and a new issue email announcement that we send every two months.

No, thanks!