Bird
0
0

How do you access the element 'green' in this PHP array?

easy📝 Conceptual Q2 of 15
PHP - Arrays
How do you access the element 'green' in this PHP array?
$colors = ['fruits' => ['apple', 'banana'], 'vegetables' => ['carrot', 'green']];
A$colors['vegetables'][1]
B$colors['fruits'][1]
C$colors[1]['green']
D$colors['green']
Step-by-Step Solution
Solution:
  1. Step 1: Identify the key and index for 'green'

    'green' is inside the 'vegetables' array at index 1.
  2. Step 2: Access the element using correct keys

    Use $colors['vegetables'][1] to get 'green'.
  3. Final Answer:

    $colors['vegetables'][1] -> Option A
  4. Quick Check:

    Access nested array by key then index = $colors['vegetables'][1] [OK]
Quick Trick: Use key then index to access nested array elements [OK]
Common Mistakes:
  • Using wrong key like 'fruits' instead of 'vegetables'
  • Trying to access by value instead of index
  • Using numeric keys on associative arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes