Reputation: 1839
I have a dynamically generated list of URL's from our internal network. For each URL, I want to:
The IP of course is UNIQUE in the list of IP's, so is there a recommended way to initially store the list of IP values so I can supply it with a an IP (key) and get back the associated server (value)?
I've looked at multidimensional arrays, or even brute force -- just create an array for each individual server's IP list -- but that seems inefficient.
Here is the PHP I want to have (psuedo):
$IPServerList = array(0 => array(ip=>1.2.3.4,server=>"server1"),
1 => array(ip=>2.1.3.4,server=>"server2"),
2 => array(ip=>3.1.3.3,server=>"server1"));
getServer("url1.mycoolurl.com");
function getServer($url) {
$ip = gethostbyname($url);
"Search $IPServerList for this $ip and return the 'server' value"
//
}
Are there any specific ways I should be storing the IP/Server list? Any recommended built in PHP functions to do the search? Any help is appreciated!
Upvotes: 0
Views: 591
Reputation: 239462
I'm not seeing any obvious reason for avoiding associative arrays:
$IPServerList = array(
'1.2.3.4' => "server1",
'2.1.3.4' => "server2",
'3.1.3.3' => "server1",
);
Arrays in PHP double has hashs/maps/dictionaries, depending on what you're used to calling them. The point is, you can use any unique string/number for your array index, and since you seem to have a 1-to-1 mapping of IPs to server names, this seems ideal.
I don't think you're likely going to find a faster way in PHP to access your data, and you can't beat it for simplicity:
if (array_key_exists($IPServerList, '192.168.1.1')) {
echo $IPServerList['192.168.1.1'];
}
The PHP manual on arrays will be useful.
Upvotes: 3
Reputation: 29170
This is the best scheme I can think of is something like this.
$servers= array(
'xxx.xxx.xxx.001' => array(
'name' => "server1",
'os' => "Windows"
),
'xxx.xxx.xxx.002' => array(
'name' => "server2",
'os' => "Linux"
),
'xxx.xxx.xxx.003' => array(
'name' => "server3",
'os' => "Mac"
)
);
Doing something like this allows you to store more information about server other than its name, as well as access settings using the IP address without looping.
$ip = 'xxx.xxx.xxx.002';
if ($servers[$ip]){
$serverName = $servers[$ip]['name'];
$serverOs = $servers[$ip]['os'];
}
Upvotes: 1
Reputation: 485
If you use arrays to store the data, as in your example you could use in_array to check if it is there.
http://php.net/manual/en/function.in-array.php
You might also check out array_search() and array_key_exists()
Upvotes: 0
Reputation: 360782
How about just keying the array off the ips?
$IPServerList = array(
'1.2.3.4' => 'server1',
'2.3.4.5' => 'server2',
'3.4.5.6' => 'server3'
);
PHP's arrays are a mishmash of both array and hashes, and support using both types of keys simultaneously, so there's no reason to generate a unique numeric key when the IP would serve as well:
echo $IPServerList['1.2.3.4']; // produces 'server1'
And if you needed to store more than just the name, you can nest the arrays:
$IPServerList['1.2.3.4'] = array('name' => 'server1', 'this' => 'x', 'that' => 'y')
Upvotes: 0
Reputation: 41070
// IP to search
$ip = '127.0.0.1';
foreach ($IPServerList as $entry) {
if ($entry['ip'] == $ip) {
echo 'MATCH :) ';
var_dump($entry);
break;
}
}
Upvotes: 0