Multidimensional Arrays
So far we’ve limited our discussion to one-dimensional arrays, which are akin to a single row or a single column in a spreadsheet. But what if we want to create the equivalent of a spreadsheet with both rows and columns? We need a second dimension. ActionScript natively supports only one-dimensional arrays, but we can simulate a multidimensional array by creating arrays within arrays. That is, we can create an array that contains elements that are themselves arrays (sometimes called nested arrays).
The simplest type of multidimensional array is a two-dimensional array, in which elements are organized conceptually into a grid of rows and columns—the rows are the first dimension of the array, and the columns are the second.
Let’s consider how a two-dimensional array works with a practical example. Suppose we’re processing an order that contains three products, each with a quantity and a price. We want to simulate a spreadsheet with three rows (one for each product) and two columns (one for the quantity and one for the price). We create a separate array for each row, treating the elements as columns:
var row1 = [6, 2.99]; // Quantity 6, Price 2.99 var row2 = [4, 9.99]; // Quantity 4, Price 9.99 var row3 = [1, 59.99]; // Quantity 1, Price 59.99
Next, we place the rows into a container array named
spreadsheet:
var spreadsheet = [row1, row2, row3];
Now we can find the total cost of the order by multiplying the quantity and price of each row and adding them all together. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access