Jump to content

IF tests on arrays ... please,please help


al

Recommended Posts

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

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..

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>";
                     }

                }


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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.