0
0
PHPprogramming~10 mins

Multidimensional arrays in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aarray(3, 4)
Barray(7, 8)
Carray(5, 6)
Darray(9, 10)
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.
2fill in blank
medium

Complete 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'
A[2][1]
B[1][2]
C[0][2]
D[2][2]
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up row and column indexes.
Using 1-based indexes instead of 0-based.
3fill in blank
hard

Fix 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'
Aarray(5, 6)
B5, 6
C[5, 6]
D"5, 6"
Attempts:
3 left
💡 Hint
Common Mistakes
Adding values without wrapping them in array().
Using string instead of array for the new row.
4fill in blank
hard

Fill 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'
A0
B1
Cnull
D"0"
Attempts:
3 left
💡 Hint
Common Mistakes
Using string "0" instead of integer 0.
Using 1 or null instead of 0.
5fill in blank
hard

Fill 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'
A90
B80
C75
D95
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of numbers for scores.
Mixing up scores between students or subjects.