detecting empty child arrays - invalid foreach

149 Views Asked by At

I'm trying to grab the NS and A records for a list of domains I have in a table.

I've started to write this:

$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 is the id and domains from the table that I want to check.

The Warnings I am getting are:

Warning: Invalid argument supplied for foreach() for the later foreach

And

Warning: dns_get_record(): DNS Query failed for dns_get_record

By the look of it I am getting these errors when dns_get_record() does not find anything.

I am trying to mark these domains as having an issue in the database so I need a method to detect them. I've tried empty() and other methods to detect them but everything I do brings up the php warnings above.

Is this because it's a multi-dimensional array? How do I go about doing this properly.

Thanks

2

There are 2 best solutions below

1
On BEST ANSWER

As the format of the source array is not specified I guessed it would be similar to the array shown below - seems to work ok and very quickly returns the dns records for later processing

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

The output of which will be similar to

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

I think, although without knowing the test domain, you're getting a return value of 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.
    }
}