al Posted January 11, 2010 Share Posted January 11, 2010 I have a if statement inside foreach loop with one problem I'm banging my head for a while now. I would like to test (IF ALL Authoritative DNS servers responded successful to query then print (RESPONSE OK) ONLY ONCE IF ALL SERVERS RESPONDED OK ELSE IF NOT print only server which did not responded). thanks. ////////////////////////// // Test 1/////////////// ///////////////////////// foreach ($response->authority as $k => $result) { // Get Authoritative DNS servers $authns = array(gethostbyname($result->nsdname)); // Initiate Authoritative DNS servers for query $dnsservers = new Net_DNS_Resolver(array('nameservers' => $authns)); // Query Authoritative DNS servers one by one $responsedns = $dnsservers->rawQuery($domain); // Get an array of DNS Authoritative servers which responded OK $resns = array($responsedns->answerfrom); // IF ALL Authoritative DNS servers responded successful to query then print (RESPONSE OK) ONLY ONCE // ELSE IF NOT print only server which did not responded) if ($authns == $resns){ echo "<font size=2 color=green> (response ok)</font><br>"; } else { echo $result->nsdname; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/188059-if-statement-inside-foreach-loop-headache/ Share on other sites More sharing options...
JonnoTheDev Posted January 11, 2010 Share Posted January 11, 2010 This line is bad. You cannot compare 2 arrays. You must compare values if ($authns == $resns){ Try this <?php $results['success'] = 0; $results['failure'] = array(); foreach($response->authority as $k => $result) { // Get Authoritative DNS servers $authns = array(gethostbyname($result->nsdname)); // Initiate Authoritative DNS servers for query $dnsservers = new Net_DNS_Resolver(array('nameservers' => $authns)); // Query Authoritative DNS servers one by one $responsedns = $dnsservers->rawQuery($domain); // Get an array of DNS Authoritative servers which responded OK $resns = array($responsedns->answerfrom); //if ($authns == $resns){ if($authns[0] == $resns[0]) { $results['success']++; } else { $results['failure'][] = $result->nsdname; } } // print the results print "<h1>Results</h1>"; print "OK Responses: ".$results['success']."<br />"; print "Failed Responses: ".count($results['failure'])."<br />".implode("<br />", $results['failure']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/188059-if-statement-inside-foreach-loop-headache/#findComment-992860 Share on other sites More sharing options...
al Posted January 12, 2010 Author Share Posted January 12, 2010 Thank you Neil I'm very grateful for this. Quote Link to comment https://forums.phpfreaks.com/topic/188059-if-statement-inside-foreach-loop-headache/#findComment-993402 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.