The basic format of the json_decode function is as follows:
$data = json_decode($jsonString, true);
The first parameter is the JSON string.
When the second parameter is true , the result will be parsed into an associative array; if omitted or false , the object will be returned.
Suppose we have the following multidimensional JSON data:
{
"user": {
"name": "Alice",
"contacts": [
{"type": "email", "value": "[email protected]"},
{"type": "phone", "value": "123456789"}
]
}
}
Parse code:
$json = '{"user":{"name":"Alice","contacts":[{"type":"email","value":"[email protected]"},{"type":"phone","value":"123456789"}]}}';
$data = json_decode($json, true);
At this point, $data is a multi-dimensional associative array. The problems many developers have encountered are:
Not correctly judged whether JSON parsing was successful <br> If the format of the incoming JSON is incorrect, json_decode will return null and will not report an error. It needs to be judged using json_last_error() .
Obfuscated object and array access method <br> When the second parameter of json_decode is false (default), the object is returned, and the -> access attribute is required; if true , the array is returned, and [] access is required.
Hierarchical access of multidimensional arrays is not considered <br> Writing $data['user']->name when accessing a multidimensional array will cause an error because $data['user'] is an array, not an object.
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo "JSON Parsing error:" . json_last_error_msg();
exit;
}
This step can effectively avoid subsequent processing errors.
// Error demonstration(The second parameter istrue,The result is an array,The ____ does not work ->)
echo $data['user']->name; // Will report an error
// Correct demonstration
echo $data['user']['name']; // Output Alice
Suppose we want to iterate over contacts :
foreach ($data['user']['contacts'] as $contact) {
echo $contact['type'] . ": " . $contact['value'] . "\n";
}
For JSON with complex structures and non-fixed layers, recursive parsing may be required. Example:
function printJson($data, $indent = 0) {
if (is_array($data)) {
foreach ($data as $key => $value) {
echo str_repeat(" ", $indent) . $key . ": ";
if (is_array($value)) {
echo "\n";
printJson($value, $indent + 1);
} else {
echo $value . "\n";
}
}
}
}
printJson($data);
This code clearly prints all levels and key values of a multidimensional array.
When parsing multidimensional JSON using json_decode , be sure to check whether the parsing is successful and use json_last_error() .
Clearly define the second parameter passed in, decide whether to return an object or an array, and the access method must be consistent.
For multi-dimensional arrays or objects, access levels accurately and avoid mixing -> and [] .
Use recursive functions to handle complex multidimensional structures when needed.
As long as you master the above points, you can effectively avoid the common traps of json_decode when parsing multi-dimensional JSON data and write more robust and maintainable PHP code.