fork(1) download
  1. <?php
  2.  
  3. $locations = array (
  4. Array (
  5. "L1ID" => 2 ,
  6. "L1Location" => 'USA' ,
  7. "L2ID" => 3 ,
  8. "L2Location" => 'New York',
  9. "L3ID" => 4 ,
  10. "L3Location" => 'Manhanttan',
  11. ) ,
  12. Array (
  13. "L1ID" => 2 ,
  14. "L1Location" => 'USA' ,
  15. "L2ID" => 8 ,
  16. "L2Location" => 'New Jersey' ,
  17. "L3ID" => 7 ,
  18. "L3Location" => 'Bergen County',
  19. ) ,
  20.  
  21. Array (
  22. "L1ID" => 5 ,
  23. "L1Location" => 'Canada' ,
  24. "L2ID" => 12,
  25. "L2Location" => 'Ontario' ,
  26. "L3ID" => 50,
  27. "L3Location" => 'Toronto' ,
  28. ) ,
  29. Array (
  30. "L1ID" => 6 ,
  31. "L1Location" => 'South Korea',
  32. "L2ID" => 22,
  33. "L2Location" => 'Gyungido' ,
  34. "L3ID" => 25,
  35. "L3Location" => 'Buchon',
  36. ) ,
  37. );
  38. $countries = array();
  39. $states = array();
  40. $cities = array();
  41. foreach ($locations as $location) {
  42. if(!array_key_exists($location['L1ID'], $countries))
  43. $countries[$location['L1ID']] = array(
  44. 'name' => $location['L1Location'],
  45. 'states' => array(),
  46. );
  47. if(!array_key_exists($location['L2ID'], $countries[$location['L1ID']]['states'])){
  48. $countries[$location['L1ID']]['states'][$location['L2ID']] = array(
  49. 'name' => $location['L2Location'],
  50. 'cities' => array(),
  51. );
  52. }
  53. if(!array_key_exists($location['L3ID'], $countries[$location['L1ID']]['states'][$location['L2ID']])){
  54. $countries[$location['L1ID']]['states'][$location['L2ID']]['cities'][$location['L3ID']] = $location['L3Location'];
  55. }
  56. }
  57.  
  58. // Generate $countries box
  59. foreach ($countries as $key => $country) {
  60. echo ($key.'<BR>'."\n");
  61. echo '<option value="' . $key . '">' . $country['name'].'</option>'."\n";
  62.  
  63. // Generate $states box
  64. foreach ($country['states'] as $key => $state) {
  65. echo ($key.'<BR>'."\n");
  66. echo '<option value="' . $key . '">' . $state['name'].'</option>'."\n";
  67.  
  68. // Generate $city box
  69. foreach ($state['cities'] as $key => $city) {
  70. echo ($key.'<BR>'."\n");
  71. echo '<option value="' . $key . '">' . $city.'</option>'."\n";
  72. }
  73.  
  74. }
  75.  
  76. }
  77.  
  78. ?>
  79.  
Success #stdin #stdout 0.03s 13112KB
stdin
Standard input is empty
stdout
        2<BR>
<option value="2">USA</option>
3<BR>
<option value="3">New York</option>
4<BR>
<option value="4">Manhanttan</option>
8<BR>
<option value="8">New Jersey</option>
7<BR>
<option value="7">Bergen County</option>
5<BR>
<option value="5">Canada</option>
12<BR>
<option value="12">Ontario</option>
50<BR>
<option value="50">Toronto</option>
6<BR>
<option value="6">South Korea</option>
22<BR>
<option value="22">Gyungido</option>
25<BR>
<option value="25">Buchon</option>