Give the perfect holiday gift for tech-minded kids: 25% off! Learn More

dark mode light mode Search Menu


Bohman on Flickr

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)