Help With Arrays :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Help With Arrays :(

Post 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!
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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;
											}
										}
?>
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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;
										}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

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