al Posted January 20, 2010 Share Posted January 20, 2010 I have this $serials query which is displaying arrays containing different serials. I'm going nuts for a few days for how to test the first displayed array with the rest of them. (if all are equal print OK, if first array is larger then others print warning, if first is smaller then others print error) If anybody among you PHP gurus can help me .... :'( $serials = array($getserials->answer[0]->serial); echo "<pre>"; print_r($serials); echo "<pre>"; Array ( [0] => 1404233 ) Array ( [0] => 1404238 ) Array ( [0] => 1404233 ) Array ( [0] => 1404232 ) Link to comment https://forums.phpfreaks.com/topic/189161-if-tests-on-arrays-pleaseplease-help/ Share on other sites More sharing options...
Buddski Posted January 20, 2010 Share Posted January 20, 2010 Is that 1 array? it looks like 4 seperate ones.. it would be better if they were all contained in 1 array.. Also im guessing this $serials = array($getserials->answer[0]->serial); is getting the serial number is that in a loop or anything.. if you can show some more code that would be good.. Link to comment https://forums.phpfreaks.com/topic/189161-if-tests-on-arrays-pleaseplease-help/#findComment-998672 Share on other sites More sharing options...
al Posted January 20, 2010 Author Share Posted January 20, 2010 It is displaying multiple arrays beacause of foreach loop: if ($response) { foreach ($response->authority as $k => $result) { // Get Authoritative DNS $dnsservers = new Net_DNS_Resolver(array('nameservers' => array(gethostbyname($response->authority[$k]->nsdname)))); // Query DNS servers one by one $getserials = $dnsservers->query($domain,SOA); // Initiate serials as arrays $serials= array($getserials->answer[0]->serial); // Print echo "<pre>"; print_r($serials); echo "<pre>"; } } Link to comment https://forums.phpfreaks.com/topic/189161-if-tests-on-arrays-pleaseplease-help/#findComment-998676 Share on other sites More sharing options...
Buddski Posted January 20, 2010 Share Posted January 20, 2010 Is this what you are after.. if ($response) { $first = null; $error = array(); foreach ($response->authority as $k => $result) { // Get Authoritative DNS $dnsservers = new Net_DNS_Resolver(array('nameservers' => array(gethostbyname($response->authority[$k]->nsdname)))); // Query DNS servers one by one $getserials = $dnsservers->query($domain,SOA); // Initiate serials array $serials = array($getserials->answer[0]->serial); if (is_null($first)) { $first = $serials; } elseif ($first != $serials){ $error[] = $serials; } } if (count($error) == 0) { echo 'OK'; } else { echo 'Found '.count($error).' problems'; } } This stores the ones that dont match in the $error variable Link to comment https://forums.phpfreaks.com/topic/189161-if-tests-on-arrays-pleaseplease-help/#findComment-998680 Share on other sites More sharing options...
al Posted January 20, 2010 Author Share Posted January 20, 2010 Yes, yes Thank you Buddski. But is it possible to test for all three tests? - if all serials are equal (OK) - if first serial is larger then others (WARNING) - if first serial is smaller then others (ERROR) Link to comment https://forums.phpfreaks.com/topic/189161-if-tests-on-arrays-pleaseplease-help/#findComment-998685 Share on other sites More sharing options...
Buddski Posted January 20, 2010 Share Posted January 20, 2010 if ($response) { $first = null; $error = array('errors'=>array(),'WARNING'=>0,'ERROR'=>0); foreach ($response->authority as $k => $result) { // Get Authoritative DNS $dnsservers = new Net_DNS_Resolver(array('nameservers' => array(gethostbyname($response->authority[$k]->nsdname)))); // Query DNS servers one by one $getserials = $dnsservers->query($domain,SOA); // Initiate serials array $serials = array($getserials->answer[0]->serial); if (is_null($first)) { $first = $serials; } elseif ($first != $serials) { $error['errors'][] = $serials; $error[($first > $serials ? 'WARNING' : 'ERROR')]++; } } if (count($error['errors']) == 0){ echo 'OK'; } else { echo 'Found '.count($error['errors']).' problems.<br/>'; echo $error['WARNING'] . 'WARNINGS<br/>'; echo $error['ERROR'] . 'ERRORS<br/>'; } } Good for you? Link to comment https://forums.phpfreaks.com/topic/189161-if-tests-on-arrays-pleaseplease-help/#findComment-998686 Share on other sites More sharing options...
al Posted January 20, 2010 Author Share Posted January 20, 2010 Buddski you are a life saver and I thank you with respect for sharing your knowledge! Al Link to comment https://forums.phpfreaks.com/topic/189161-if-tests-on-arrays-pleaseplease-help/#findComment-998700 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.