countries = array('norway' => array('Oslo', 'Trondheim', 'Bergen', 'Halden', 'Sarpsborg', 'Hammerfest'), 'sweden' => array('Stockholm', 'Gothenburg', 'Karlstad'), 'england' => array('London', 'Newcastle', 'Bath', 'Liverpool')); if(isset($_GET['country'])) { $this->findCitiesByCountry(trim($_GET['country'])); } if(isset($_POST['city'])) { $this->findCountryByCity(trim($_GET['city'])); } } public function findCitiesByCountry($country) { $cities = 'Not found'; $country = strtolower($country); if(isset($this->countries[$country])) { $cities = $this->countries[$country]; } $this->displayJSONResult($cities); } public function findCountryByCity($city) { $country = 'Not found'; $break = false; foreach($this->countries as $key => $val) { for($i = 0, $count = count($val); $i < $count; $i++) { if($city == $val[$i]) { $country = $key; $break = true; break; } } if($break) { break; } } $this->displayJSONResult($country); } } $obj = new ExampleServer(); ?>