为json数据生成所需的输出

时间:2012-11-02 07:18:11

标签: php html json

{"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"}}

我可以使用以下代码显示输出:

$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>

现在,我需要对其进行排序,以便显示如下表格:

<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>

我无法确定如何以可用于生成此表的方式对响应进行排序,使用响应数据将工具提示添加到ADD链接(如dinakar.com)。

2 个答案:

答案 0 :(得分:2)

$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>";                                             
  }

答案 1 :(得分:0)

看起来您在解码响应时没有问题,而是将其标准化为您需要显示的内容。为此,您需要从域字符串中提取TLD,除非您提前知道。大概你这样做,因为它曾用于请求响应开始?

无论如何,下面的代码说明了将其放入适合您传递给视图的数组的一种方法(或者您正在这样做):

$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);
}

这是我的头脑。生成的$domains数组看起来像这样(为简洁而截断):

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

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

请注意,我在这里并没有做很多的理智,但这应该足以帮助你沉浸其中。然后,您只需将表格从密钥中移开,并使用响应中返回的所需信息填充添加链接。