Page 1 of 1

Help With Arrays :(

Posted: Thu Nov 07, 2002 5:01 pm
by JPlush76
Hey guys,
I'm trying to get a better grasp on arrays and I'm having some trouble

basically what I want to do is create and array of Canadian Postal Codes(first letter only)

Code: Select all

<?php
$can_array = array ("A" => "NF", "B"=> "NS","C" => "PE");
?>
I'll use 3 codes as an example...

I take the incoming postal code and substring the first letter, what I want to do is if that first letter is in the array and matches the province that the user entered.... return true else if the first letter is not in the array or it doesn't equal the value return false.

I'm not sure what the best way to do this is :(

any help? thanks!

Posted: Thu Nov 07, 2002 5:20 pm
by JPlush76
here is what I have come up with just now and it looks like it works

is there any problem doing it this way?

Code: Select all

<?php
$can_array = array ("A" => "NF", "B"=> "NS", "C" => "PE");
										
										foreach($can_array as $key => $value)
										{
											if($canzip == $key AND $field3 == $value)
											{
												return TRUE;
											} else {
												
												return false;
											}
										}
?>

Posted: Thu Nov 07, 2002 5:42 pm
by JPlush76
bleh that only works if the first key matches, if the first key doesn't match it always returns false :(


here is some nice sloppy code to get around it.. there has to be a better way

Code: Select all

<?php
$can_array = array (
    "A" => "NF", "B" => "NS", "C" => "PE", "E" => "NB", "G" => "QC",
     "H" => "QC", "J" => "QC","K" => "ON", "L" => "ON", "M" => "ON",
     "N" =>	"ON", "P" => "ON", "R" => "MB", "S" => "SK", "T" => "AB", 
    "V" => "BC", "X" => "NT", "Y" => "YT"
);
										
										foreach($can_array as $key => $value)
												{
													if($canzip == $key AND $field3 == $value)
													{
														return TRUE;
													} else {
													$c++;
													}
												}
										if($c = count($can_array))
										{
						
										return false;
										}
?>

Posted: Thu Nov 07, 2002 6:14 pm
by volka

Code: Select all

$bValid = isset($can_arrayї$canzip]) && $can_arrayї$canzip]==$field3;
?