How can JSON within JSON be identified and resolved in PHP?

To identify and resolve JSON within JSON in PHP, you can use the json_decode function with the second parameter set to true to decode the JSON string into an associative array. Then, you can recursively check each value in the array to see if it is a JSON string and decode it accordingly.

function resolveNestedJson($data) {
    $decoded = json_decode($data, true);

    if (is_array($decoded)) {
        foreach ($decoded as $key => $value) {
            if (is_string($value) && is_array(json_decode($value, true))) {
                $decoded[$key] = resolveNestedJson($value);
            }
        }
    }

    return $decoded;
}

// Example usage
$jsonString = '{"key1": "value1", "key2": "{\"nestedKey\": \"nestedValue\"}"}';
$resolvedData = resolveNestedJson($jsonString);

print_r($resolvedData);