Home » Php » php – Passing key and using an array inside of a function

php – Passing key and using an array inside of a function

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a function defined like this:

    public static function getRegionFromState( $theState){
      //Returns Region from the State
      //Region I: CT DC DE IL IN MA MD ME MI NH NJ NY OH PA RI VT WI
      //Region II: AL AR FL GA KY LA MS NC PR SC TN TX VA VI WV
      //Region III: AK AZ CA CO GU HI IA ID KS MN MO MP MT ND NE NM NV OK OR SD UT WA WY

      //States and Region assignments
 $statesRegions = array
  (
    'Connecticut'=> array( 'ST'=>"CT" ,'Region'=>"Region I"),
    'District of Columbia'=> array( 'ST'=>"DC" ,'Region'=>"Region I"),
    'Delaware'=> array( 'ST'=>"DE" ,'Region'=>"Region I"),
    'Illinois'=> array( 'ST'=>"IL" ,'Region'=>"Region I"),
    'Indiana'=> array( 'ST'=>"IN" ,'Region'=>"Region I"),
    'Massachusetts'=> array( 'ST'=>"MA" ,'Region'=>"Region I"),
    'Maryland'=> array( 'ST'=>"MD" ,'Region'=>"Region I"),
    'Maine'=> array( 'ST'=>"ME" ,'Region'=>"Region I"),
    'Michigan'=> array( 'ST'=>"MI" ,'Region'=>"Region I"),
    'New Hampshire'=> array( 'ST'=>"NH" ,'Region'=>"Region I"),
    'New Jersey '=> array( 'ST'=>"NJ" ,'Region'=>"Region I"),
    'New York'=> array( 'ST'=>"NY" ,'Region'=>"Region I"),
    'Ohio'=> array( 'ST'=>"OH" ,'Region'=>"Region I"),
    'Pennsylvania'=> array( 'ST'=>"PA" ,'Region'=>"Region I"),
    'Rhode Island'=> array( 'ST'=>"RI" ,'Region'=>"Region I"),
    'Vermont'=> array( 'ST'=>"VT" ,'Region'=>"Region I"),
    'Wisconsin'=> array( 'ST'=>"WI" ,'Region'=>"Region I"),
    'Alabama'=> array( 'ST'=>" AL" ,'Region'=>" Region II"),
    'Arkansas'=> array( 'ST'=>" AR" ,'Region'=>" Region II"),
    'Florida'=> array( 'ST'=>" FL" ,'Region'=>" Region II"),
    'Georgia'=> array( 'ST'=>" GA" ,'Region'=>" Region II"),
    'Kentucky'=> array( 'ST'=>" KY" ,'Region'=>" Region II"),
    'Louisiana'=> array( 'ST'=>" LA" ,'Region'=>" Region II"),
    'Mississippi'=> array( 'ST'=>" MS" ,'Region'=>" Region II"),
    'North Carolina '=> array( 'ST'=>" NC" ,'Region'=>" Region II"),
    'Puerto Rico'=> array( 'ST'=>" PR" ,'Region'=>" Region II"),
    'South Carolina '=> array( 'ST'=>" SC" ,'Region'=>" Region II"),
    'Tennessee'=> array( 'ST'=>" TN" ,'Region'=>" Region II"),
    'Texas'=> array( 'ST'=>" TX" ,'Region'=>" Region II"),
    'Virginia'=> array( 'ST'=>" VA" ,'Region'=>" Region II"),
    'Virgin Islands '=> array( 'ST'=>" VI" ,'Region'=>" Region II"),
    'West Virginia'=> array( 'ST'=>" WV" ,'Region'=>" Region II"),
    'Alaska '=> array( 'ST'=>" AK" ,'Region'=>" Region III"),
    'Arizona'=> array( 'ST'=>" AZ" ,'Region'=>" Region III"),
    'California '=> array( 'ST'=>" CA" ,'Region'=>" Region III"),
    'Colorado'=> array( 'ST'=>" CO" ,'Region'=>" Region III"),
    'Guam'=> array( 'ST'=>" GU" ,'Region'=>" Region III"),
    'Hawaii '=> array( 'ST'=>" HI" ,'Region'=>" Region III"),
    'Iowa'=> array( 'ST'=>" IA" ,'Region'=>" Region III"),
    'Idaho'=> array( 'ST'=>" ID" ,'Region'=>" Region III"),
    'Kansas '=> array( 'ST'=>" KS" ,'Region'=>" Region III"),
    'Minnesota'=> array( 'ST'=>" MN" ,'Region'=>" Region III"),
    'Missouri'=> array( 'ST'=>" MO" ,'Region'=>" Region III"),
    'Northern Marianas Islands'=> array( 'ST'=>" MP" ,'Region'=>" Region III"),
    'Montana'=> array( 'ST'=>" MT" ,'Region'=>" Region III"),
    'North Dakota'=> array( 'ST'=>" ND" ,'Region'=>" Region III"),
    'Nebraska'=> array( 'ST'=>" NE" ,'Region'=>" Region III"),
    'New Mexico '=> array( 'ST'=>" NM" ,'Region'=>" Region III"),
    'Nevada '=> array( 'ST'=>" NV" ,'Region'=>" Region III"),
    'Oklahoma'=> array( 'ST'=>" OK" ,'Region'=>" Region III"),
    'Oregon '=> array( 'ST'=>" OR" ,'Region'=>" Region III"),
    'South Dakota'=> array( 'ST'=>" SD" ,'Region'=>" Region III"),
    'Utah'=> array( 'ST'=>" UT" ,'Region'=>" Region III"),
    'Washington '=> array( 'ST'=>" WA" ,'Region'=>" Region III"),
    'Wyoming'=> array( 'ST'=>" WY" ,'Region'=>" Region III")
  );

      return $statesRegions[$theState]['Region'];

    }

If I call this as:

  $theState = $theUserObj->profile['State']; 
  $theRegion = self::sss_getRegionFromState($theState); 

Then I get this:Notice: Undefined index: Pennsylvania in E:\moodle\moodle\local\sssevents\classes\observer.php on line 169

But if I do:

  $myState = 'Wyoming'; 
  echo( self::sss_getRegionFromState($myState); 

Then this works correctly. So, I guess that the question is … why is $myState working, but $theState which is passed in as a parameter to the function not working correctly?

I can’t figure out how to fix this.

Thanks!

How to&Answers: