0
{"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)。

4

2 回答 2

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>";                                             
  }
于 2012-11-02T07:24:38.807 回答
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
        )

请注意,我在这里并没有做很多理智的事情,但这应该足以帮助您沉迷其中。然后,您只需让您的表格远离按键,并使用响应中返回的任何他们需要的信息填充您的添加链接。

于 2012-11-02T08:12:33.887 回答