[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-general
Subject:    RES: [PHP] Array help.
From:       "Samuel Lopes Grigolato" <samuel.grigolato () gmail ! com>
Date:       2012-10-24 17:40:45
Message-ID: 06bc01cdb20e$b55debd0$2019c370$ () gmail ! com
[Download RAW message or body]

Could you try changing this:

if($groupTest != FALSE) {

to this:

if($groupTest !== FALSE) {

?

-----Mensagem original-----
De: Paul Halliday [mailto:paul.halliday@gmail.com] 
Enviada em: quarta-feira, 24 de outubro de 2012 15:38
Para: PHP-General
Assunto: [PHP] Array help.

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/

--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic