How to compare a zip code within an array with multiple ranges in PHP?

Asked

Viewed 174 times

1

Eae personal!

I’m needing a hint on how to filter a zip code into a list that will have multiple ranges, if (IF) the zip code is inside that list, run an HTML that has some strings in PHP.

Basically the test CEP would be 33100-100 And on the ZIP list would be for example:

01000-000...01599-999
02000-000...02099-999
08000-000...08099-999
06300-000...06399-999
00640-000...06499-999

As the CEP 33100-100 does not participate in the list, nothing would appear. If we tried with CEP 01099-999 it would accept and execute HTML.

The problem is that the CEP list is relatively messy and has several different ranges. At the moment it is already 85 intervals, and maybe it will increase in the future, so I wanted something practical to facilitate the inclusion of new intervals in the future. Does anyone have any idea?

1 answer

1


I think it may be so:

//cep a ser encontrado
$cep="03100-100";
//array com os intervalos dos ceps
$array=array(
    array("01000-000","01599-999"),
    array("02000-000","02099-999"),
    array("08000-000","08099-999"),
    array("06300-000","06399-999"),
    array("00640-000","06499-999")
);
//foreach para buscar dentro do array de intervalos
foreach($array as $key=>$value){
    //$value assume o papel dos arrays contidos dentro do array principal, sendo assim o offset [0] para o inicio do intervalo e [1] para o fim
    if($cep >= $value[0] && $cep <= $value[1]){
        echo "bingo ".$key;
        break;
    }else{
        echo "desculpe cep não encontrado";
    }
}

Sorry for the mess, it was just for you to have an idea of what to do. I hope I helped.

  • 1

    Thank you! Gave it right :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.