# PHP: Multidimensional Arrays II

Aims:

• to understand the difference between rectangular 2-dimensional arrays and ragged 2-dimensional arrays
• to practice writing programs for both rectangular and ragged 2-dimensional arrays

# Class exercise

• A relief map, showing height above mean sea level (in metres), for a 100 square metre area of Outer Nerdland can be represented by a 100 × 100 2-dimensional array
• E.g. this can be represented as an array called `\$map`:  100 100 103 104 103 100 100 103 104 103 ... 100 100 105 104 103 108 99 98 97 96 ... 100 101 104 104 103 106 100 99 98 97 ... 100 100 103 103 102 105 104 103 102 101 ... ... ... ... ... ... ... ... ... ... ... ...
• A peak is a cell that is higher than all of its immediate neighbouring cells to the north, east, south and west
• Write a PHP script that counts the number of peaks in `\$map`
• Hint: use `for`-loops, not `foreach`-loops

# Rectangular arrays

• The multidimensional arrays we have seen so far are sometimes called rectangular: the 'inner' arrays are all of the same length

# Ragged arrays

• However, there is nothing to prevent the 'inner' arrays from being of different lengths
• In this case, the multidimensional arrays are sometimes called ragged
• E.g. Donna Kebab is studying Witchcraft at the National University of Nerdland, where some modules have more pieces of coursework than others:
```\$donnas_grades = array
(
'WC1101' => array(85,  0,  0,  0),
'WC1102' => array(72, 75, 91, 60, 34, 35),
'WC1103' => array(57, 57, 57, 57, 57)
);```

# Class exercises

1. Write a PHP script that outputs Donna's grades as nested unordered lists, as follows:
• WC1101
• 85
• 0
• WC1102
2. Write a PHP script that outputs Donna's grades as a table. Make all rows in the table the same length by 'padding' them with cells that contains hyphens (`<td>-</td>`), as follows:
 WC1101 WC1102 WC1103 85 0 0 0 - - 72 75 91 60 34 35 57 57 57 57 57 -