/ Published in: PHP
Takes a Singapore Postal Code and outputs its corresponding district name
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php //Example dco_get_district("160149"); /* ################################## CONVERT POSTAL CODE TO DISTRICT NUMBER AND NAME ################################## */ function dco_get_district($postal) { //Define the 28 Districts '01', '02', '03', '04', '05', '06' ), '07', '08' ), '14', '15', '16' ), '09', '10' ), '11', '12', '13' ), '17' ), '18', '19' ), '20', '21' ), '22', '23' ), '24', '25', '26', '27' ), '28', '29', '30' ), '31', '32', '33' ), '34', '35', '36', '37' ), '38', '39', '40', '41' ), '42', '43', '44', '45' ), '46', '47', '48' ), '49', '50', '81' ), '51', '52' ), '53', '54', '55', '82' ), '56', '57' ), '58', '59' ), '60', '61', '62', '63', '64' ), '65', '66', '67', '68' ), '69', '70', '71', ), '72', '73' ), '77', '78' ), '75', '76' ), '79', '80' ) ); //Districts are determined by first 2 digits of the postal code //Find corresponding district foreach( $districts as $districtName=>$digits ) { foreach( $digits as $digit) { if( $digit == $firstTwoDigits) { return $districtName; } } } } ?>