Bird
0
0

Given this multidimensional array representing students and their scores:

hard📝 Application Q15 of 15
PHP - Arrays
Given this multidimensional array representing students and their scores:
$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?
A$avg = array_sum($students[2]['scores']) / count($students[2]['scores']);<br>echo $avg;
B$avg = array_sum($students['Bob']['scores']) / count($students['Bob']['scores']);<br>echo $avg;
C$avg = array_sum($students[1]['score']) / count($students[1]['score']);<br>echo $avg;
D$avg = array_sum($students[1]['scores']) / count($students[1]['scores']);<br>echo $avg;
Step-by-Step Solution
Solution:
  1. Step 1: Locate Bob's data in the array

    Bob is at index 1, and his scores are under the key 'scores'.
  2. 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.
  3. Final Answer:

    $avg = array_sum($students[1]['scores']) / count($students[1]['scores']); echo $avg; -> Option D
  4. Quick Check:

    Index 1 and 'scores' key used correctly [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes