【问题标题】:Neostrada api, foreach the dns resultsNeostrada api,foreach dns 结果
【发布时间】:2020-02-21 14:51:48
【问题描述】:

我不明白如何处理这个响应,所以它显示所有记录而不是完整的“结果”。

我的结果:

{"results":[{"id":83213964,"domainId":648668,"name":"pc-gear.nl","type":"SOA","content":"een.dnssrv.nl hostmaster@neostrada.nl 2019102501","ttl":3600,"prio":0},{"id":83213965,"domainId":648668,"name":"pc-gear.nl","type":"NS","content":"een.dnssrv.nl","ttl":3600,"prio":0},{"id":83213966,"domainId":648668,"name":"pc-gear.nl","type":"NS","content":"twee.dnssrv.nl","ttl":3600,"prio":0},{"id":83213968,"domainId":648668,"name":"pc-gear.nl","type":"A","content":"185.87.187.226","ttl":3600,"prio":0},{"id":83213969,"domainId":648668,"name":"localhost.pc-gear.nl","type":"A","content":"127.0.0.1","ttl":3600,"prio":0},{"id":83213970,"domainId":648668,"name":"*.pc-gear.nl","type":"A","content":"185.87.187.226","ttl":3600,"prio":0},{"id":83213971,"domainId":648668,"name":"www.pc-gear.nl","type":"A","content":"185.87.187.226","ttl":3600,"prio":0},{"id":83213972,"domainId":648668,"name":"mail.pc-gear.nl","type":"A","content":"185.87.187.226","ttl":3600,"prio":0},{"id":83213973,"domainId":648668,"name":"webmail.pc-gear.nl","type":"A","content":"185.87.187.226","ttl":3600,"prio":0},{"id":83213974,"domainId":648668,"name":"cpanel.pc-gear.nl","type":"A","content":"185.87.187.226","ttl":3600,"prio":0},{"id":83213975,"domainId":648668,"name":"pc-gear.nl","type":"AAAA","content":"2a00:f10:305:0:1c00:9fff:fe00:51a","ttl":3600,"prio":0},{"id":83213976,"domainId":648668,"name":"*.pc-gear.nl","type":"AAAA","content":"2a00:f10:305:0:1c00:9fff:fe00:51a","ttl":3600,"prio":0},{"id":83213977,"domainId":648668,"name":"www.pc-gear.nl","type":"AAAA","content":"2a00:f10:305:0:1c00:9fff:fe00:51a","ttl":3600,"prio":0},{"id":83213978,"domainId":648668,"name":"mail.pc-gear.nl","type":"AAAA","content":"2a00:f10:305:0:1c00:9fff:fe00:51a","ttl":3600,"prio":0},{"id":83213979,"domainId":648668,"name":"_dmarc.pc-gear.nl","type":"TXT","content":"v=DMARC1; p=none;","ttl":3600,"prio":0},{"id":83213980,"domainId":648668,"name":"pc-gear.nl","type":"MX","content":"mail.pc-gear.nl","ttl":3600,"prio":10},{"id":83213981,"domainId":648668,"name":"pc-gear.nl","type":"TXT","content":"v=spf1 a mx include:spf.totaalholding.nl ip4:185.87.187.226 -all","ttl":3600,"prio":0},{"id":92619388,"domainId":648668,"name":"pc-gear.nl","type":"NS","content":"drie.dnssrv.nl","ttl":3600,"prio":0}]}

我的代码: 只是 api 和 echo $reuslt。

如果您有任何问题,请尽管提问。实际上我不知道该放什么。

【问题讨论】:

  • foreach(json_decode($result,true) as $value)

标签: php arrays json string multidimensional-array


【解决方案1】:

$result是一个json字符串,你需要用json_decode解析成数组,然后你就可以将它作为一个数组来访问。 Demo

foreach(json_decode($result,true) as $value){
    print_r($value);
}

或访问特定值

$data = json_decode($result,true);
echo $data['results'][0]['type'];

【讨论】:

  • 谢谢,我现在怎样才能只显示例如 dns 的类型?
  • @Kevko 在我的回答中查看演示。
  • 但我的意思是每一个像“结果”这样的单曲都显示 dns 的类型,而不仅仅是 1 个单曲。
  • print_r(array_column($data['results'],'type')); 3v4l.org/djNK0
  • 但是有没有办法把它放在一个 foreach 中,它只输出答案而不是整个数组?
【解决方案2】:

您可以使用array_walk_recursive

 $jarr = json_decode($result, true);
 array_walk_recursive($jarr['results'], function($v,$k){
  echo $k.'--'.$v;echo '<hr>';
 });

表格格式

$data = json_decode($result,true);
$table = '<table>';
array_walk_recursive($data['results'], function($v,$k) use(&$table){
  $table .= "<tr><td>{$k}</td><td>{$v}</td><tr>";
 });
$table .= '</table>';

echo $table;

【讨论】:

  • 如何把它放在一张桌子上?
猜你喜欢
  • 2019-08-01
  • 2016-05-03
  • 1970-01-01
  • 2018-07-13
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多