【问题标题】:detecting empty child arrays - invalid foreach检测空子数组 - 无效的 foreach
【发布时间】:2018-03-14 15:37:48
【问题描述】:

我正在尝试获取表中域列表的 NS 和 A 记录。

我已经开始写这个了:

$domains = GetDomainsForDNS();

foreach ($domains as $domain){
  $domain_id = $domain[0];
  $domain = $domain[1];
  $dns_records = dns_get_record($domain, DNS_NS + DNS_A);

  echo $domain;
  foreach($dns_records as $dns_record){
    if (!$dns_record){
      //var_dump($dns_record);
      echo "empty";
    }
  }
}

$domains 是我要检查的表中的 id 和域。

我收到的警告是:

警告:为后面的 foreach 提供的 foreach() 参数无效

警告:dns_get_record():对 dns_get_record 的 DNS 查询失败

当 dns_get_record() 没有找到任何东西时,我会收到这些错误。

我正在尝试将这些域标记为数据库中存在问题,因此我需要一种方法来检测它们。我尝试了 empty() 和其他方法来检测它们,但我所做的一切都会引发上面的 php 警告。

这是因为它是一个多维数组吗?我该如何正确地做到这一点。

谢谢

【问题讨论】:

  • dns_get_record(): 可以返回 false,这是 foreach 的无效输入
  • 您能否添加源数据的样本 - 即:$domains
  • 问题当然是dns_get_record。先测试一下结果。
  • @RamRaider 没问题,数据样本:Array ( [0] => Array ( [id] => 64 [0] => 64 [domain] => wistia.net [1] => wistia.net ) [1] => Array ( [id] => 249 [0] => 249 [domain] => applianceadventures.com [1] => applianceadventures.com ) [2] => Array ( [id] => 378 [0] => 378 [domain] => paintballpursuit.com [1] => paintballpursuit.com ))
  • 感谢您 - 很难阅读,但没关系。毫无疑问,如果需要,您可以从下面的代码中挑选想法;-)

标签: php arrays error-handling is-empty dns-get-record


【解决方案1】:

由于未指定源数组的格式,我猜它会类似于下面显示的数组 - 似乎工作正常,并且很快返回 dns 记录以供以后处理

function GetDomainsForDNS(){
    /* example dummy data */
    return array(
        array(1,'stackoverflow.com'),
        array(2,'google.com'),
        array(3,'microsoft.com'),
        array(4,'yellow-banana.com'),
        array(5,'yahoo.com'),
        array(6,'blue-velvet-caulifower.org')
    );
}


$domains = GetDomainsForDNS();
$dns = array();


foreach( $domains as $arr ){
    try{
        $id = $arr[0];
        $domain = $arr[1];

        /* suppress potential errors */
        $records = @dns_get_record( $domain, DNS_NS + DNS_A );

        /* If the query failed, throw a catchable exception */
        if( empty( $records ) ) throw new Exception( sprintf( 'DNS Query failed for %s', $domain ) );

        /* add records to output array or later use */
        $dns[ $domain ]=$records;

    }catch( Exception $e){
        /* display warnings */
        printf( '%s<br />', $e->getMessage() );
        /* move to the next domain to check */
        continue;
    }
}

printf( '<pre>%s</pre>',print_r( $dns, true ) );

其输出将类似于

DNS Query failed for yellow-banana.com
DNS Query failed for blue-velvet-caulifower.org
Array
(
    [stackoverflow.com] => Array
        (
            [0] => Array
                (
                    [host] => stackoverflow.com
                    [type] => A
                    [ip] => 151.101.193.69
                    [class] => IN
                    [ttl] => 1
                )
                ............ etc

【讨论】:

  • 效果很好,太棒了!我有很多东西要学,谢谢你的帮助
【解决方案2】:

我认为,虽然不知道测试域,但您会得到 false 的返回值

//check returned values not falsey
if ($dns_records) {
    // as the returned value is not false
    foreach ($dns_records as $dns_record) {
        // $dns_record is an associative array.
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多