340 likes | 1.21k Views
Multidimensional Arrays. CIT 336. The Basics. Explaining Multidimensional Arrays. Arrays. An array is a serial collection of data. In most programming languages, including PHP, individual array elements are accessed using the following syntax: variableName [index] Ex: $ boolArr [2] = true;
E N D
Multidimensional Arrays CIT 336
The Basics Explaining Multidimensional Arrays
Arrays An array is a serial collection of data. In most programming languages, including PHP, individual array elements are accessed using the following syntax: variableName[index] Ex: $boolArr[2] = true; In the example above, the 3rd item in the $boolArr array is being accessed (indexes are 0-based, so the first item is accessed at index: 0, the second item at index: 1, and so on.)
2D Arrays A ‘normal’ array is one dimensional, meaning it has elements ranging from index:0 to index:n. However, arrays do not necessarily need to be limited to one dimension. We could represent a 2D array by using two indexers like this: variableName[index1][index2] Ex: $intArr2D[1][2] = 2; Notice, there are now two indexers, giving our array two dimensionality. The next slide demonstrates this using a table to illustrate. This concept can be expanded to any number of dimensions, for example, a three dimensional array would be accessed like this: $doubleArr3D[0][9][6] = 3.4f;
2D Array - Rectangular • This is an example of a “rectangular” array – an array of arrays where the nested arrays are all the same size (in this case, each nested array contains 3 elements).
Jagged Arrays Another type of multidimensional array is a Jagged Array. A jagged array is also an array of arrays, but in this case, the nested arrays may each be of different lengths. Consider the example to the right – $jagged[0] contains 3 elements, but $jagged[1] only contains 2 elements, while $jagged[2] contains 5 elements. This variation in nested element length is why the this sort of array is called a Jagged Array.
2D Array - Jagged • This is an example of a “jagged” array – an array of arrays where the nested arrays can be of varying sizes.
Creating & Accessing Multidimensional Arrays • The process for creating a multidimensional array is very similar to the process for creating a single dimensional array. Essentially, a “normal” array is first created, and then arrays are created and assigned as elements of that array. For example: $myArray = array(); $myArray[0] = array(); $myArray[1] = array(); ... $myArray[99] = array(); • This demonstrates manually assigning each nested array, but a for loop could certainly be used. • Elements of multidimensional arrays are accessed just like one dimensional arrays, only additional indexers are used. $board[0][0] = false; $cell[15][6] = 16.4; $names[3]['first'] = 'Don';
Application Using Multidimensional Arrays
When do we use them…? • There are a number of cases where multidimensional arrays are the natural choice for a data structure. For instance, in the case of modeling a chess board, a two dimensional array makes a lot of sense; a chess board is really a 2D array of squares and representing it with a 2D array of variables is pretty straightforward. • Tabular data may also be a good candidate for using a 2D array. Tables also make easy transitions to 2D arrays because they themselves our laid out in a similar fashion (rows by cols) and are easy to translate into array[row][col] structure.
Practical Example An example from our textbook that doesn’t model a typical 2D object is an online shopping cart. The shopping cart is represented as an array. In this case, it will be an array of items (themselves an array), creating a multidimensional array. $shoppingCart = array(); Then, each item is represented as an associative array. $item = array(); $item['product_name'] = 'Book'; $item['product_price'] = 15.05; $item['item_quantity'] = 1; Items are then "added to the cart" simply by adding them to the $shoppingCart array. $shoppingCart[] = $item; Additional items can easily be defined and added. The $shoppingCart array contains a series of items, and each item contains its own set of elements. This same approach can be used could be used to represent employees within a department, departments within an organization, and even organizations within groups of organizations.