PHP How to Convert JSON to Array Easily
Use
json_decode($jsonString, true) to convert a JSON string into a PHP array; the second parameter true makes the output an array instead of an object.Examples
Input{"name":"John"}
OutputArray ( [name] => John )
Input[1,2,3]
OutputArray ( [0] => 1 [1] => 2 [2] => 3 )
Inputnull
OutputNULL
How to Think About It
To convert JSON to an array in PHP, you need to decode the JSON string. The function
json_decode reads the JSON text and turns it into a PHP value. By default, it creates objects, but if you want an array, you set the second argument to true.Algorithm
1
Get the JSON string input.2
Call the JSON decoding function with the input and set the second parameter to true.3
Store the result as a PHP array.4
Use or return the array as needed.Code
php
<?php $jsonString = '{"name":"John", "age":30}'; $array = json_decode($jsonString, true); print_r($array); ?>
Output
Array
(
[name] => John
[age] => 30
)
Dry Run
Let's trace converting '{"name":"John", "age":30}' to an array.
1
Input JSON string
{"name":"John", "age":30}
2
Call json_decode with true
json_decode('{"name":"John", "age":30}', true)
3
Resulting PHP array
Array ( [name] => John [age] => 30 )
| Step | Action | Value |
|---|---|---|
| 1 | Input JSON | {"name":"John", "age":30} |
| 2 | Decode JSON to array | Array ( [name] => John [age] => 30 ) |
Why This Works
Step 1: json_decode function
The json_decode function reads a JSON string and converts it into a PHP variable.
Step 2: Second parameter true
Passing true as the second argument tells PHP to convert JSON objects into associative arrays instead of PHP objects.
Step 3: Result is PHP array
The output is a PHP array that you can use like any other array in your code.
Alternative Approaches
Convert JSON to object
php
<?php
$jsonString = '{"name":"John", "age":30}';
$object = json_decode($jsonString);
print_r($object);
?>This returns a PHP object instead of an array, which might be easier for some uses but less flexible for array functions.
Manually parse JSON (not recommended)
php
<?php // Not recommended: manual parsing is error-prone and complex $jsonString = '{"name":"John"}'; $array = []; // manual parsing would be complex ?>
Manual parsing is complicated and error-prone; always prefer json_decode.
Complexity: O(n) time, O(n) space
Time Complexity
The time depends on the length of the JSON string, as the function parses each character once.
Space Complexity
The space used grows with the size of the JSON string because it creates a new PHP array in memory.
Which Approach is Fastest?
Using json_decode with the second parameter is the fastest and most reliable way to convert JSON to an array.
| Approach | Time | Space | Best For |
|---|---|---|---|
| json_decode with true | O(n) | O(n) | Converting JSON to array efficiently |
| json_decode default (object) | O(n) | O(n) | When PHP objects are preferred |
| Manual parsing | O(n^2) or worse | O(n) | Avoid due to complexity and errors |
Always pass
true as the second argument to json_decode to get an array instead of an object.Forgetting to set the second parameter to
true causes json_decode to return an object, not an array.