Challenge - 5 Problems
Multidimensional Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested array access
What is the output of this PHP code?
PHP
<?php $array = [[1, 2], [3, 4], [5, 6]]; echo $array[1][0]; ?>
Attempts:
2 left
💡 Hint
Remember that arrays start at index 0.
✗ Incorrect
The outer array has three elements: [0]=>[1,2], [1]=>[3,4], [2]=>[5,6]. Accessing $array[1] gives [3,4], and $array[1][0] is 3.
❓ Predict Output
intermediate2:00remaining
Count elements in multidimensional array
What is the output of this PHP code?
PHP
<?php $array = [ 'fruits' => ['apple', 'banana'], 'vegetables' => ['carrot', 'pea', 'corn'] ]; echo count($array['vegetables']); ?>
Attempts:
2 left
💡 Hint
count() counts elements in the specified array.
✗ Incorrect
The 'vegetables' array has three elements: 'carrot', 'pea', and 'corn'. So count($array['vegetables']) returns 3.
🔧 Debug
advanced2:00remaining
Identify the error in multidimensional array access
What error does this PHP code produce?
PHP
<?php $array = [[1, 2], [3, 4]]; echo $array[2][0]; ?>
Attempts:
2 left
💡 Hint
Check if the index exists before accessing.
✗ Incorrect
The outer array has indexes 0 and 1 only. Accessing $array[2] causes an undefined offset error.
📝 Syntax
advanced2:00remaining
Which option correctly defines a multidimensional array?
Which of the following PHP code snippets correctly defines a multidimensional array with two rows and two columns?
Attempts:
2 left
💡 Hint
Look for proper array syntax and separators.
✗ Incorrect
Option B uses correct PHP array syntax with nested arrays separated by commas. Option B uses semicolon inside array which is invalid. Option B is a flat array, not multidimensional. Option B misses a comma between 3 and 4.
🚀 Application
expert3:00remaining
Calculate sum of all elements in a multidimensional array
Given the multidimensional array below, what is the sum of all its integer elements?
PHP
<?php $array = [ [1, 2, 3], [4, 5], [6] ]; $sum = 0; foreach ($array as $subarray) { foreach ($subarray as $value) { $sum += $value; } } echo $sum; ?>
Attempts:
2 left
💡 Hint
Add all numbers: 1+2+3+4+5+6.
✗ Incorrect
The sum is 1+2+3+4+5+6 = 21.