PHP: Multidimensional Arrays I
Derek Bridge
Department of Computer Science,
University College Cork
PHP: Multidimensional Arrays I
Aims:
- to see examples of multidimensional arrays (arrays of arrays)
- to practice writing programs that use nested
for
-loops to handle multidimensional arrays
Multidimensional arrays
- In an indexed array,
- elements are indexed by integers
- elements may be integers, floats, strings, Booleans,...
and even arrays
- In an associative array,
- the keys are strings
- the values may be integers, floats, strings, Booleans,...
and even arrays
- An array that contains other arrays is called a multidimensional array
- Specifically, an array that contains arrays of simple values
is called a 2-dimensional array
- Higher dimensional arrays (e.g. 3-dimensional arrays, i.e. arrays of
arrays or arrays of values) are possible
2-dimensional arrays: example I
- E.g. Hugh Jeegoh is studying Alchemy at the National University
of Nerdland and uses a multidimensional array to record his
coursework grades in modules AL1101, AL1102 and AL1103
respectively:
$hughs_grades = array
(
array(72, 75, 91, 60),
array(85, 0, 0, 0),
array(57, 57, 57, 57)
);
2-dimensional arrays: example II
- 2-dimensional tables, matrices and grids are often represented
using 2-dimensional arrays
- Most commonly, the array contains an array for each row of the table/matrix/grid
- E.g. the matrix on the left can be represented using the array on the right
| | | |
$a = array
(
array(12, 15, 0, 22),
array(38, 14, 38, 19),
array(0, 6, 90, 19)
);
|
12 | 15 | 0 | 22 |
38 | 14 | 38 | 19 |
0 | 6 | 90 | 19 |
| | | |
- Use
$a[$i][$j]
to access the element in the
$i
th row, $j
th column
$a[0][0] | $a[0][1] | $a[0][2] | $a[0][3] |
$a[1][0] | $a[1][1] | $a[1][2] | $a[1][3] |
$a[2][0] | $a[2][1] | $a[2][2] | $a[2][3] |
- Class exercise: What is the value of
$a[2][1]
?
How would you access the value in column 3 of row 0?
Class exercise: accessing the elements
What do you think $x
,
$y
and $z
will contain after the following assignment
statements:
$x = $hughs_grades[1];
$y = $x[0];
$z = $hughs_grades[1][0];
Class exercise: accessing the elements
Write statements to perform
the following:
- assign Hugh's AL1101 grades into
$AL1101_grades
- assign his grade for the second piece of AL1102 coursework into
$AL1102_lab2_grade
- echo his grade for the last piece of AL1103 coursework
Class exercise
- Write PHP to output Hugh's grades as a table
- Modify your PHP so that it also computes and echoes the
average grade for each module
2-dimensional arrays: example III
- E.g. Ann O Domini stores her grades, like Hugh does, but she
realises she gets a more
self-explanatory representation if she uses an associative array of
indexed arrays:
$anns_grades = array
(
'AL1101' => array(100, 95, 90, 85),
'AL1102' => array(50, 51, 51, 52),
'AL1103' => array(60, 55, 60, 65)
);
Class exercise: accessing the elements
Write statements to perform
the following:
- assign Ann's AL1101 grades into
$AL1101_grades
- assign her grade for the second piece of AL1102 coursework into
$AL1102_lab2_grade
- echo her grade for the last piece of AL1103 coursework
Class exercise
Write PHP to output the grades as a table but include
the module codes as row headers