Multi-dimensional Arrays

Our last topic – multi dimensional arrays

Arrays

Advanced Type

We said above that an array can store variables and those variables can store data. But what about the following scenario – where we have spaceships where we want to store the spaceship’s SIZE in diameter and their DISTANCE from Earth? 

See the table below of 3 spaceships – Falcon Z, Mon and Space X

Can we do this? 

Arrays which store other arrays are known as Multi-Dimensional Arrays. These will become very useful as your data and programme becomes more complex. Watch the video below to know how to create a multidimensional array.

The video below is to help you understand multi-dimensional arrays. A Recap of the video follows

Let’s create a multi dimensional array that stores spaceships. And we want to store their SIZE in diameter and their DISTANCE from Earth.

Let’s play around with arrays by clicking on the button below. You’ll see they created an array consisting of different types of spaceships with their size and speed.

Let’s play around with arrays by clicking on the button below. You’ll see they created an array consisting of different types of spaceships with their size and speed. 

Your task: 

  1. change the names of spaceships to the names of the spaceships of your choice  
  2. Change the values for the spaceship sizes and speed. 

Be creative and play around. Click run to see what happens.

Task: Now it is the time to work on the drones

Remember – Agent Gail needs your help to programme the drones to locate Galaxy X flight. 

The drones need to store all the coordinates that are sent to the agents from the drones – this will track their flight path. Your task as the software engineer of the drones is to implement code which stores all the coordinates sent by the drones. 

Below is the table of the coordinates sent by the drones:

Here are some steps to guide you:

  • Create an array named coordinates 

  • Store the coordinates listed above for each date, using a multidimensional array (2 values per element in the array) 

Here’s a hint: the first item in the array would be: 

$coordinates = array (

  array(“20 December 2040”,-0.97377,-45.64867),

Click “fork me” to add and edit your PHP code.

Once you’re done, take a screenshot and save it as JPEG. Upload it to the assignment upload below. Well done – we have completed the first step in helping Agent Gail locate the Galaxy X flight by tracking the drones flight path.

In this section, we talked about  arrays and how they help us create a single collection of  values of different data types .We introduced the following 

  • Arrays
  • How to create arrays
  • How to access an element in an array
  • Associate arrays
  • Multidimensional arrays

You modified the storage system of the drones to save all the coordinates that are sent to the agents – this will help to trace the drones flight path. So that when the drones find Galaxy X, it’s an easy job to find them.

Time to complete the quiz before moving onto Decision structures.

Next: We'll learn about decision making in PHP