0
0
PHPprogramming~20 mins

Multidimensional arrays in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multidimensional Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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];
?>
A5
B2
C4
D3
Attempts:
2 left
💡 Hint
Remember that arrays start at index 0.
Predict Output
intermediate
2: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']);
?>
A5
B2
C3
D1
Attempts:
2 left
💡 Hint
count() counts elements in the specified array.
🔧 Debug
advanced
2: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];
?>
AUndefined offset: 2
BParse error: syntax error
CFatal error: Cannot use [] for reading
DNotice: Undefined variable
Attempts:
2 left
💡 Hint
Check if the index exists before accessing.
📝 Syntax
advanced
2: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?
A$array = [[1, 2], [3 4]];
B$array = [[1, 2], [3, 4]];
C$array = [1, 2; 3, 4];
D$array = array(1, 2, 3, 4);
Attempts:
2 left
💡 Hint
Look for proper array syntax and separators.
🚀 Application
expert
3: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;
?>
A21
B20
C18
D19
Attempts:
2 left
💡 Hint
Add all numbers: 1+2+3+4+5+6.