Re: Mystery foreach error

From: Date: Wed, 13 Mar 2013 00:16:36 +0000
Subject: Re: Mystery foreach error
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-320503@lists.php.net to get a copy of this message
Angela Barone wrote: > I think I figured it out. > > <?php > $states = array( > 'AL' => array( > '350','351','352','353', ), > 'AK' => array( > '995','996','997','998','999', ), > 'AZ' => array( > '850','851','852','853','854', ), > 'WI' => array( '530','531','532', ), > 'WY' => array( > '820','821','822','823','824', ), > ); > > $zip = 35261; > $state = 'XX'; > > $zip_short = substr($zip, 0, 3); > foreach ($states[$state] as $zip_prefix) { > if ($zip_prefix == $zip_short) { > echo "State = $state"; > } else { > echo 'no'; > } > } > ?> > > Running this script, I got the same error as before. If $state is a known > state abbreviation in the array, everything is fine, but if someone was to > enter, say 'XX' like I did above or leave it blank, then the error is > produced. I placed an if statement around the foreach loop to test for > that and I'll keep an eye on it. > > Thank you for getting me to look at the array again, which led me to look > at the State. > > Angela Presumably there is a fixed list of State - those are US states? - so why not provide a drop down list of the possible choices? -- Cheers David Robley "I need to be careful not to add too much water," Tom said with great concentration.

« previous php.general (#320503) next »