skip to Main Content

I have following JSON response in PHP

{
    "tlds": {
        ".art": {
            "register": "16.99",
            "transfer": "16.99",
            "renew": "16.99",
            "transfer_requires_epp_code": true,
            "id_protection_supported": true
        },
        ".icu": {
            "register": "12.99",
            "transfer": "12.99",
            "renew": "12.99",
            "transfer_requires_epp_code": true,
            "id_protection_supported": true
        },
        ".xyz": {
            "register": "2.99",
            "transfer": "13.99",
            "renew": "13.99",
            "transfer_requires_epp_code": true,
            "id_protection_supported": true
        },
        ".cyou": {
            "register": "12.99",
            "transfer": "12.99",
            "renew": "12.99",
            "transfer_requires_epp_code": true,
            "id_protection_supported": true
        },
        ".fr": {
            "register": "12.99",
            "transfer": "12.99",
            "renew": "12.99",
            "transfer_requires_epp_code": true,
            "id_protection_supported": false
        },
        ".be": {
            "register": "7.99",
            "transfer": "7.99",
            "renew": "12.99",
            "transfer_requires_epp_code": false,
            "id_protection_supported": false
        },
        ".ca": {
            "register": "8.99",
            "transfer": "8.99",
            "renew": "9.99",
            "transfer_requires_epp_code": true,
            "id_protection_supported": false
        },
        ".ch": {
            "register": "8.99",
            "transfer": null,
            "renew": "13.99",
            "transfer_requires_epp_code": true,
            "id_protection_supported": true
        }
    },
    "currency_code": "USD",
    "message": "Successfully retrieved prices",
    "fgt_id": "xxxxxxxxxxxxxxxxx"
}

And following code to echo in php the result

$response = curl_exec($curl);
$decoded_json = json_decode($response, true);
echo "<table><tr><td>Extension</td><td>Register</td><td>Transfer</td><td>Renew</td></tr>";
foreach ($decoded_json['tlds'] as $key => $value) {
    echo "<tr><td>".$decoded_json['tlds']."</td><td>".$value['register']."</td><td>".$value['transfer']."</td><td>".$value['renew']."</td><tr>";
}
echo "</table>";

But that doesn’t work. It breaks the page. I only mentionned 8 domainname extensions, but there are much more in the real response. This means we have to consider the domainname extensions as unknown keys. How to echo the response data taking into accout that the tlds has many rows with the first key unknown?

2

Answers


  1. In the echo statement, change

    .$decoded_json['tlds'].
    

    to

    .$key.
    

    This will show the TLD in the first column of the table.

    Login or Signup to reply.
  2. Parse the JSON tld key using foreach and the $key will be each tld.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search