I have a dynamically generated list of URL’s from our internal network. For each URL, I want to:
- Find the IP of the URL.
- Compare the IP to a list of IP’s and their associated server.
- Return the server associated with the IP.
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!
I’m not seeing any obvious reason for avoiding associative arrays:
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:
The PHP manual on arrays will be useful.