Array help.
| From: | Paul Halliday | Date: | Wed, 24 Oct 2012 17:38:01 +0000 |
| Subject: | Array help. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-319583@lists.php.net to get a copy of this message | ||
I am processing v4IP's and what I want to do is a prefix substitution
if the 3rd octet matches a predefined list $groupMappings. I went down
this path and it isn't working as expected. Drawing a blank on this
one. Why does 40 miss the comparison?
$hostname = "Z";
$ips =
array('10.1.40.1','10.1.41.1','10.1.1.1','10.1.40.1','10.9.1.1');
foreach ($ips as $ip) {
$groupMappings = array('40' =>'A','41'
=>'B','1' =>'C');
$ocTest = explode(".", $ip);
$groupKeys = array_keys($groupMappings);
$groupTest = array_search("$ocTest[2]", $groupKeys);
if($groupTest != FALSE) {
$hostGroup = $groupMappings[$groupKeys[$groupTest]];
echo "Hit! $ip : $hostname : $hostGroup\n";
} else {
$hostGroup = substr($hostname, 0,2);
echo "Miss! $ip : $hostname : $hostGroup\n";
}
}
Miss! 10.1.40.1 : Z : Z
Hit! 10.1.41.1 : Z : B
Hit! 10.1.1.1 : Z : C
Miss! 10.1.40.1 : Z : Z
Hit! 10.9.1.1 : Z : C
Thanks!
--
Paul Halliday
http://www.pintumbler.org/