Bird
0
0

Which of the following is the correct syntax to decode a JSON string into an associative array in PHP?

easy📝 Syntax Q3 of 15
PHP - File Handling
Which of the following is the correct syntax to decode a JSON string into an associative array in PHP?
A$arr = json_decode($json);
B$arr = json_decode($json, true);
C$arr = json_encode($json, true);
D$arr = json_encode($json);
Step-by-Step Solution
Solution:
  1. Step 1: Recall json_decode parameters

    The second parameter of json_decode() controls whether the result is an associative array (true) or an object (false or omitted).
  2. Step 2: Identify correct syntax

    $arr = json_decode($json, true); correctly passes true as the second argument to get an associative array.
  3. Final Answer:

    $arr = json_decode($json, true); -> Option B
  4. Quick Check:

    json_decode with true = associative array [OK]
Quick Trick: Pass true as second argument to get associative array [OK]
Common Mistakes:
  • Omitting the second argument and expecting an array
  • Using json_encode instead of json_decode

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes