Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a 2D array with two rows.
PHP
<?php
$matrix = array(
array(1, 2),
[1]
);
print_r($matrix);
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single value instead of an array for the second row.
Forgetting to use the array() function for the second row.
✗ Incorrect
The second row of the 2D array is created with array(5, 6).
2fill in blank
mediumComplete the code to access the element '8' from the 2D array.
PHP
<?php
$matrix = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
echo $matrix[1];
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up row and column indexes.
Using 1-based indexes instead of 0-based.
✗ Incorrect
The element '8' is in the third row (index 2) and second column (index 1), so $matrix[2][1] accesses it.
3fill in blank
hardFix the error in the code to correctly add a new row to the multidimensional array.
PHP
<?php
$matrix = array(
array(1, 2),
array(3, 4)
);
$matrix[] = [1];
print_r($matrix);
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding values without wrapping them in array().
Using string instead of array for the new row.
✗ Incorrect
To add a new row, you must add an array. So array(5, 6) is correct.
4fill in blank
hardFill both blanks to create a 3x3 matrix with all zeros.
PHP
<?php
$matrix = array(
array([1], [2], 0),
array(0, 0, 0),
array(0, 0, 0)
);
print_r($matrix);
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string "0" instead of integer 0.
Using 1 or null instead of 0.
✗ Incorrect
To create a matrix with all zeros, use 0 for each element.
5fill in blank
hardFill all three blanks to create an associative multidimensional array representing students and their scores.
PHP
<?php
$students = array(
"Alice" => array("Math" => [1], "Science" => [2]),
"Bob" => array("Math" => [3], "Science" => 85)
);
print_r($students);
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of numbers for scores.
Mixing up scores between students or subjects.
✗ Incorrect
Alice's Math score is 95, Science is 80, and Bob's Math score is 75.