sorted_countries as $rarray ) { array_push( $list, $rarray[0] ); array_push( $list2, $rarray[1] ); } return array_combine( $list2, $list ); } // returns a list of all states public function states() { $list = []; foreach ( $this->sorted_countries as $rarray ) { $t = explode( '|', $rarray[5] ); foreach ( $t as $m ) { array_push( $list, $m ); } } sort( $list ); return $list; } // getStates accepts a name, iso_2_code, iso_3_code, iso_num_code or iso_something_code of a country public function getStates( $country = null ) { if ( !empty( $country ) && ( is_string( $country ) || is_numeric( $country ) ) ) { $list = []; $list2 = []; $country = strtoupper( $country ); foreach ( $this->sorted_countries as $rarray ) { if ( ( is_string( $country ) && ( ( strlen( $country ) > 3 && strtolower( $rarray[0] ) == strtolower( $country ) ) || ( strlen( $country ) == 2 && strtoupper( $rarray[1] ) == $country ) || ( strlen( $country ) == 3 && strtoupper( $rarray[2] ) == $country ) || ( strlen( $country ) > 3 && strtoupper( str_replace( ' ', '', $rarray[4] ) ) == str_replace( ' ', '', $country ) ) ) ) || ( is_numeric( $country ) && $rarray[3] == $country ) ) { $t = explode( '|', $rarray[5] ); for ( $i=0; $i < sizeof( $t ); $i++ ) { array_push( $list, $t[$i] ); $string = str_replace( ' ', '', $t[$i] ); array_push( $list2, strtolower( $string ) ); } } } return array_combine( $list2, $list ); } else { return []; } } // getCountry accepts a name, iso_2_code, iso_3_code, iso_num_code or iso_something_code of a country public function getCountry( $country = null ) { if ( !empty( $country ) && ( is_string( $country ) || is_numeric( $country ) ) ) { $list = []; $country = strtoupper( $country ); foreach ( $this->sorted_countries as $rarray ) { if ( ( is_string( $country ) && ( ( strlen( $country ) > 3 && strtolower( $rarray[0] ) == strtolower( $country ) ) || ( strlen( $country ) == 2 && strtoupper( $rarray[1] ) == $country ) || ( strlen( $country ) == 3 && strtoupper( $rarray[2] ) == $country ) || ( strlen( $country ) > 3 && strtoupper( str_replace( ' ', '', $rarray[4] ) ) == str_replace( ' ', '', $country ) ) ) ) || ( is_numeric( $country ) && $rarray[3] == $country ) ) { $last = sizeof( $rarray ) - 1; $list = array_combine( ['name', 'iso2', 'iso3', 'iso_num', 'something_code', 'dial_code', 'states'], [$rarray[0], $rarray[1], $rarray[2], $rarray[3], $rarray[4], $rarray[$last], $this->getStates( $rarray[1] )] ); } } sort( $list ); return $list; } else { return []; } } // findState accepts only name of state public function findState( $state = null ) { if ( !empty( $state ) && is_string( $state ) ) { $list = []; $state = strtolower( $state ); foreach ( $this->sorted_countries as $rarray ) { if ( preg_match( "@\b{$state}\b@", strtolower( $rarray[5] ) ) ) { //array_push($list, $rarray[0]); $last = sizeof( $rarray ) - 1; array_push( $list, array_combine( ['country', 'iso2', 'iso3', 'iso_num', 'something_code', 'dial_code'], [$rarray[0], $rarray[1], $rarray[2], $rarray[3], $rarray[4], $rarray[$last]] ) ); } } sort( $list ); return $list; } else { return []; } } public function getStateName( $state, $country ) { $states = $this->getStates( $country ); foreach ( $states as $key => $value ) { if ( $key == $state || $value == $state ) { return $value; } } } public function isCountry( $state ) { $state = $this->getCountry( $state ); if ( !empty( $state ) ) { return true; } return false; } public function getCountryCode( $country = null ) { foreach ( $this->sorted_countries as $rarray ) { if ( $country == $rarray[0] ) { return $rarray[1]; } } } }