Jump to content

Linking 2 arrays after successful ping in PHP


Go to solution Solved by cyberRobot,

Recommended Posts

Hi,

Basically I have written this PHP script to ping address on my network and then I would like to add the name that defines that IP after my image if successful or not.

I am struggling on the name part can anyone assist?

My code is below:

<?php
$systems = array("192.168.9.254","192.168.9.205","192.168.9.200","192.168.9.201","192.168.9.1","192.168.9.2","192.168.9.3","192.168.9.4","192.168.9.7","192.168.9.13");
$Sysname = array("Item1","Item2","Item3","Item4","Item5","Item6","Item7","Item8","Item9");
$good = "Received = 1";
$successValue;
//echo "<h1>Site Status  ".date("h:i:s")."</h1>";
echo "<br><br>";
foreach ($systems as $ip) {
    unset($result);
    $successValue = "DOWN";
    exec("ping -n 1 $ip", $result);
    foreach($result as $line) {
        if (strpos($line,$good) == TRUE){
            $successValue = "UP";
        }
    }
    echo "IP Address: ".$ip." ";
    If ($successValue == "UP") {
         echo "System is ".$successValue. "&nbsp;"."<img src='/Images/GTick.jpg'>"."&nbsp;";

    }else{
            echo "System is ".$successValue. "&nbsp;"."<img src='/Images/RTick.jpg'>"."&nbsp;";
    }
echo "<br><br>";
//var_dump($result);
}
?>

Cheers James

 

  • Solution

I would switch to a multidimensional associative array for your data. For example, you could have something like this

<?php
$systems = array(
	array('ip'=>'192.168.9.254', 'name'=>'Item1'),
	array('ip'=>'192.168.9.205', 'name'=>'Item2'),
	
	//...remaining systems
	
);
?>

 

That way all your data is in the same place. Then your loop could use the values like this:

<?php
foreach ($systems as $currSystem) {
	echo $currSystem['ip'];
	echo $currSystem['name'];
}
?>

 

I was going to suggest combining the arrays

$systems = array_combine($systems, $Sysname);

then you could

foreach ($systems as $ip => $name) {
    // ping
}

However, to that that you would need the same number of names as there are IPs. It would give

Array
(
    [192.168.9.254] => Item1
    [192.168.9.205] => Item2
    [192.168.9.200] => Item3
    [192.168.9.201] => Item4
    [192.168.9.1] => Item5
    [192.168.9.2] => Item6
    [192.168.9.3] => Item7
    [192.168.9.4] => Item8
    [192.168.9.7] => Item9
    [192.168.9.13] => Item10
)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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