Jump to content

[SOLVED] Array Help - Multidimensional array


jmanfffreak

Recommended Posts

Hi,

 

I've been knee deep in documentation everywhere and I can't find how to do this.... I'm working on a server status script, with all the configs in an array. Here is the configuration file:

 

<?php
$server = 
array(
'1' => array(
'ServerName' => 'Foo',
'ServerIP' => '127.0.0.1',
'ServerPort' => '9999',
'ServerDB' => 'Foo'
),
'2' => array(
'ServerName' => 'Bar',
'ServerIP' => '127.0.0.1',
'ServerPort' => '9998',
'ServerDB' => 'Bar'
),
);
?>

The enduser of this program will be able to add and subtract as many servers from this config as they wanted,  all they'd have to do is copy the second array and change the array number from 1 to 2, or 2 to 3 or w/e. So it will need to loop through each array and return everything in order to make a function for it to do. The only problem is it has to loop and return each array as a seperate entity.

In sense, the end results I want to look something like this:

Server Status

Foo: ONLINE

Bar: OFFLINE

 

Here's what I was thinking should be done, (aka this is what code I had) but unsure if it's right or what I should do:

 

<?php

function ServerStatus()
{
foreach ( $server as $val1 ) 
{
foreach ( $val1 as $val2 )
{
...........
}
}
}
?>

 

But unsure of what to do next.

 

Thanks for any help you can provide me.

 

Link to comment
Share on other sites

As far as looping out the information goes, surely you just want something like...

 

<?php
echo '<b>Server Status:</b><br/>';
foreach($server as $s) {
  echo $s['ServerName'] . ': Status<br/>';
}
?>

 

I'm not sure on how you plan on checking the status, is it a value in the array or is that the point of the question?

Link to comment
Share on other sites

Well, basically, I'll need to extract all the information in order to actually ping the server. I guess the server name could be the key in each array, so instead of what's above, I can omit the key 'ServerName' and instead use:

<?php
$server = 
array(
'Foo' => array(
'ServerIP' => '127.0.0.1',
'ServerPort' => '9999',
'ServerDB' => 'Foo'
),
'Bar' => array(
'ServerIP' => '127.0.0.1',
'ServerPort' => '9998',
'ServerDB' => 'Bar'
),
);
?>

But pretty much I just need to know how to extract each individual array and use that information to plug into a function that will check the server status by a simple ping. I know how to do that part, I just don't know how to get the information I need to do it out of the Multidimensional array.

 

Thanks.

Link to comment
Share on other sites

<?php
$server = 
array(
'Foo' => array(
'ServerIP' => '127.0.0.1',
'ServerPort' => '9999',
'ServerDB' => 'Foo'
),
'Bar' => array(
'ServerIP' => '127.0.0.1',
'ServerPort' => '9998',
'ServerDB' => 'Bar'
),
);
?>

 

Cags got it right, didn't you test the code?

 

echo '<pre>';

   foreach($server as $s)
   {
      print_r($s, true);
      echo "\r\n";
   }

echo '</pre>';

Link to comment
Share on other sites

There are lots of ways you can access various information...

 

<?php
// access one specific servers information
$current_server_id = 'Foo';
$current_server = $server[$current_server_id];
$current_server_ip = $current_server['ServerIP'];
// loop through servers
foreach($server as $k=>$s) {
  $current_server_id = $k;
  $current_server = $s;
  $current_server_ip = $s['ServerIP'];
}
?>

 

Does that help?

Link to comment
Share on other sites

<?php
$test = socket_create(AF_INET,SOCK_STREAM);
while ($e = each($server)) {
	$status = 'off';
	if (socket_connect($test,$e['serverIP'],$e['serverPort'])) $status = 'on';
	echo "{$e['serverName']} {$status}line";
}
?>

 

oh I'd also like to note I am not completely sure you can reuse a socket resource :) but this one tries to atleast, tell me if you get any errors.

Link to comment
Share on other sites

There are lots of ways you can access various information...

 

<?php
// access one specific servers information
$current_server_id = 'Foo';
$current_server = $server[$current_server_id];
$current_server_ip = $current_server['ServerIP'];
// loop through servers
foreach($server as $k=>$s) {
  $current_server_id = $k;
  $current_server = $s;
  $current_server_ip = $s['ServerIP'];
}
?>

 

Does that help?

 

That....looks about right. Plugging it in now, I'll edit/repost with information and results. Thanks for all the input!

Link to comment
Share on other sites

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.