php - Required output generated for json data

711
{"99.net":{"status":"regthroughothers","classkey":"dotnet"},
"99.org":  {"status":"regthroughothers","classkey":"domorg"},
"99.mobi":{"status":"regthroughothers","classkey":"dotmobi"},
"99.name":{"status":"Invalid Domain Name","classkey":"dotname"},
"99.us":{"status":"regthroughothers","classkey":"domus"},
"99.com":{"status":"regthroughothers","classkey":"domcno"},
"99.info":{"status":"Invalid Domain Name","classkey":"dominfo"},
"99.co.uk":{"status":"available","classkey":"thirdleveldotuk"},
"99.biz":{"status":"Invalid Domain Name","classkey":"dombiz"},
"99.in":{"status":"Invalid Domain Name","classkey":"dotin"}}

I'm able to display the output with the following code:

$json1 = json_decode($response1);

 foreach($json1 as $key=>$sel_rows)     
  {
      echo $key ;
      echo " status: ". $sel_rows->status." ";
      echo " Class: ". $sel_rows->classkey." ";
      echo "Price";
      echo "<br>";                                             
  }<br>

Now, I need to sort it so that a table such as the following can be shown:

<table border="1">
<tr>
<td>.com</td>
<td>.net</td>
<td>.info</td>
<td>.org</td>
</tr>
<tr>
<td><a href="">ADD</a></td>
<td><a href="">ADD</a></td>
<td><a href="">ADD</a></td>
<td><a href="">ADD</a></td>
</tr>
</table>

I'm having trouble figuring out how to sort the response in a way that I can use to generate this table, using the response data to add tool tips to the ADD links (like dinakar.com).

331

Answer

Solution:

$json1 = json_decode($response1, TRUE);

 foreach($json1 as $key=>$sel_rows)     
  {
      echo $key ;
      echo " status: ". $sel_rows['status']."&nbsp;";
      echo " Class: ". $sel_rows['classkey']."&nbsp;";
      echo "Price";
      echo "<br>";                                             
  }
224

Answer

Solution:

It looks like you're not having a problem decoding the response, but rather normalizing it for what you need to display. To do that, you'll need to extract the TLD from the domain string, unless you know it ahead of time. Presumably you do, as it was used to request the response to begin with?

Anyway, the following code illustrates one way of getting it into an array suitable for you to pass to your view (or however you're doing it):

$response1 = <<< EOF
{"99.net":{"status":"regthroughothers","classkey":"dotnet"},
"99.org":  {"status":"regthroughothers","classkey":"domorg"},
"99.mobi":{"status":"regthroughothers","classkey":"dotmobi"},
"99.name":{"status":"Invalid Domain Name","classkey":"dotname"},
"99.us":{"status":"regthroughothers","classkey":"domus"},
"99.com":{"status":"regthroughothers","classkey":"domcno"},
"99.info":{"status":"Invalid Domain Name","classkey":"dominfo"},
"99.co.uk":{"status":"available","classkey":"thirdleveldotuk"},
"99.biz":{"status":"Invalid Domain Name","classkey":"dombiz"},
"99.in":{"status":"Invalid Domain Name","classkey":"dotin"}}
EOF;


function get_tld($url) {
    $host = parse_url($url);
    $domain = $host['path'];
    $tail = substr($domain, -7); // Watch out, gotcha! Be sure of this.
    $tld = strstr($tail, ".");
    return $tld;
}

$domains = array();
$json1 = json_decode($response1);

foreach ($json1 as $idx => $obj) {
    $tld = get_tld($idx);
    $domains[$tld] = array('tld' => $tld, 'status' => $obj->status, 'classkey' => $obj->classkey);
}

This is off the top of my head. The resulting$domains array looks like this (truncated for brevity):

Array
(
    [.net] => Array
        (
            [tld] => .net
            [status] => regthroughothers
            [classkey] => dotnet
        )

    [.org] => Array
        (
            [tld] => .org
            [status] => regthroughothers
            [classkey] => domorg
        )

Note, I'm not doing a whole lot of sanity here, but that should be enough to help you sink your teeth into it. You'd then just make your table head off the keys, and populate your add links with whatever information they need that was returned in the response.

People are also looking for solutions to the problem: php strtotime and date issues

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.