A multidimensional array is an array that contains one or more arrays inside it. It is like a table or grid where each element can be accessed using multiple keys or indexes.
Click to reveal answer
beginner
How do you access the element 'PHP' in this array?
$arr = [['HTML', 'CSS'], ['JavaScript', 'PHP']];
You access 'PHP' by using two indexes: $arr[1][1]. The first index selects the second array, and the second index selects the second element inside it.
Click to reveal answer
intermediate
How to add a new element 'Python' to the second array in a multidimensional array?
Use $arr[1][] = 'Python'; to add 'Python' at the end of the second array inside the multidimensional array.
Click to reveal answer
beginner
What will count($arr) return if $arr = [[1,2], [3,4], [5,6]];?
It will return 3 because count() counts the number of elements in the outer array, which has three inner arrays.
Click to reveal answer
intermediate
How can you loop through all elements in a multidimensional array?
Use nested foreach loops: the outer loop goes through each inner array, and the inner loop goes through each element inside those arrays.
Click to reveal answer
What does $arr[0][1] access in a multidimensional array?
AThe first element of the second inner array
BThe first element of the first inner array
CThe second element of the first inner array
DThe second element of the second inner array
✗ Incorrect
$arr[0] selects the first inner array, and [1] selects its second element.
How do you add a new inner array to a multidimensional array $arr?
The array starts with [[ but only one closing bracket ] is present before the semicolon.
Step 2: Identify missing bracket
The outer array requires a closing bracket ], so the code should end with ]];
Final Answer:
Missing closing bracket for the outer array -> Option B
Quick Check:
Outer array brackets must match [OK]
Hint: Count opening and closing brackets carefully [OK]
Common Mistakes:
Confusing inner and outer brackets
Ignoring syntax errors
Assuming semicolon fixes bracket issues
5. Given this multidimensional array representing students and their scores: <pre>$students = [ ['name' => 'Alice', 'scores' => [85, 90]], ['name' => 'Bob', 'scores' => [78, 82]], ['name' => 'Charlie', 'scores' => [92, 88]] ];
Which code correctly calculates and prints the average score of Bob?
hard
A. $avg = array_sum($students[2]['scores']) / count($students[2]['scores']); echo $avg;
B. $avg = array_sum($students['Bob']['scores']) / count($students['Bob']['scores']); echo $avg;
C. $avg = array_sum($students[1]['score']) / count($students[1]['score']); echo $avg;
D. $avg = array_sum($students[1]['scores']) / count($students[1]['scores']); echo $avg;
Solution
Step 1: Locate Bob's data in the array
Bob is at index 1, and his scores are under the key 'scores'.
Step 2: Calculate average correctly
Use array_sum and count on $students[1]['scores'] to get average. $avg = array_sum($students[1]['scores']) / count($students[1]['scores']); echo $avg; does this correctly.
Final Answer:
$avg = array_sum($students[1]['scores']) / count($students[1]['scores']); echo $avg; -> Option D
Quick Check:
Index 1 and 'scores' key used correctly [OK]
Hint: Use numeric index for array, string keys for inner arrays [OK]
Common Mistakes:
Using string key for outer array
Misspelling 'scores' key
Using wrong index for Bob
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.