Singapore Postal Code to District Name


/ Published in: PHP
Save to your folder(s)

Takes a Singapore Postal Code and outputs its corresponding district name


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. //Example
  4. dco_get_district("160149");
  5.  
  6. /* ##################################
  7. CONVERT POSTAL CODE TO DISTRICT NUMBER AND NAME
  8. ################################## */
  9.  
  10. function dco_get_district($postal) {
  11.  
  12. //Define the 28 Districts
  13. $districts = array(
  14. 'D01 City - Business District'=>array(
  15. '01',
  16. '02',
  17. '03',
  18. '04',
  19. '05',
  20. '06'
  21. ),
  22. 'D02 City - Business District'=>array(
  23. '07',
  24. '08'
  25. ),
  26. 'D03 Central South'=>array(
  27. '14',
  28. '15',
  29. '16'
  30. ),
  31. 'D04 South'=>array(
  32. '09',
  33. '10'
  34. ),
  35. 'D05 South West'=>array(
  36. '11',
  37. '12',
  38. '13'
  39. ),
  40. 'D06 City - Business District'=>array(
  41. '17'
  42. ),
  43. 'D07 City'=>array(
  44. '18',
  45. '19'
  46. ),
  47. 'D08 Central'=>array(
  48. '20',
  49. '21'
  50. ),
  51. 'D09 Central - Orchard'=>array(
  52. '22',
  53. '23'
  54. ),
  55. 'D10 Central - Near Orchard'=>array(
  56. '24',
  57. '25',
  58. '26',
  59. '27'
  60. ),
  61. 'D11 Central - Near Orchard'=>array(
  62. '28',
  63. '29',
  64. '30'
  65. ),
  66. 'D12 Central'=>array(
  67. '31',
  68. '32',
  69. '33'
  70. ),
  71. 'D13 Central East'=>array(
  72. '34',
  73. '35',
  74. '36',
  75. '37'
  76. ),
  77. 'D14 Central East'=>array(
  78. '38',
  79. '39',
  80. '40',
  81. '41'
  82. ),
  83. 'D15 East Coast'=>array(
  84. '42',
  85. '43',
  86. '44',
  87. '45'
  88. ),
  89. 'D16 Upper East Coast'=>array(
  90. '46',
  91. '47',
  92. '48'
  93. ),
  94. 'D17 Far East'=>array(
  95. '49',
  96. '50',
  97. '81'
  98. ),
  99. 'D18 Far East'=>array(
  100. '51',
  101. '52'
  102. ),
  103. 'D19 North East'=>array(
  104. '53',
  105. '54',
  106. '55',
  107. '82'
  108. ),
  109. 'D20 Central North'=>array(
  110. '56',
  111. '57'
  112. ),
  113. 'D21 Central West'=>array(
  114. '58',
  115. '59'
  116. ),
  117. 'D22 Far West'=>array(
  118. '60',
  119. '61',
  120. '62',
  121. '63',
  122. '64'
  123. ),
  124. 'D23 North West'=>array(
  125. '65',
  126. '66',
  127. '67',
  128. '68'
  129. ),
  130. 'D24 Far North West'=>array(
  131. '69',
  132. '70',
  133. '71',
  134. ),
  135. 'D25 Far North'=>array(
  136. '72',
  137. '73'
  138. ),
  139. 'D26 North'=>array(
  140. '77',
  141. '78'
  142. ),
  143. 'D27 Far North'=>array(
  144. '75',
  145. '76'
  146. ),
  147. 'D28 North East'=>array(
  148. '79',
  149. '80'
  150. )
  151. );
  152.  
  153. //Districts are determined by first 2 digits of the postal code
  154. $firstTwoDigits = substr($postal, 0, 2);
  155.  
  156. //Find corresponding district
  157. foreach( $districts as $districtName=>$digits )
  158. {
  159. foreach( $digits as $digit)
  160. {
  161. if( $digit == $firstTwoDigits)
  162. {
  163. return $districtName;
  164. }
  165. }
  166. }
  167.  
  168. }
  169.  
  170. ?>

URL: http://www.shophouses.sg

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.