博客列表 >第十二课 数组循环

第十二课 数组循环

清风King
清风King原创
2021年10月10日 17:56:49482浏览

1.循环省、市、区数组

  1. <?php
  2. // 导入省市区数组
  3. include_once 'arr.php';
  4. foreach ($arr as $keyProvince => $province) {
  5. echo $province['area_name'] . '<br>';
  6. foreach ($province['son'] as $keyCity => $city) {
  7. echo '----' . $city['area_name'] . '<br>';
  8. foreach ($city['son'] as $keyCity => $area) {
  9. echo '----' . '----' . '----' . $area['area_name'] . '<br>';
  10. }
  11. }
  12. }

结果:


2. 用 echo 输出单个值,用print_r()打印其中一个城市和区

  1. // 用echo打印数组中的“北京”
  2. echo $arr[0]['area_name'];

结果:北京

  1. //用print_r()打印唐山市城市和区
  2. print_r($arr[2]['son'][1]);

结果:

  1. [area_id] => 130200
  2. [area_pid] => 130000
  3. [area_name] => 唐山市
  4. [lng] => 118.175
  5. [lat] => 39.6351
  6. [son] => Array
  7. (
  8. [0] => Array
  9. (
  10. [area_id] => 130284
  11. [area_pid] => 130200
  12. [area_name] => 滦州市
  13. [lng] =>
  14. [lat] =>
  15. )
  16. [1] => Array
  17. (
  18. [area_id] => 130202
  19. [area_pid] => 130200
  20. [area_name] => 路南区
  21. [lng] => 118.154
  22. [lat] => 39.625
  23. )
  24. [2] => Array
  25. (
  26. [area_id] => 130203
  27. [area_pid] => 130200
  28. [area_name] => 路北区
  29. [lng] => 118.201
  30. [lat] => 39.6244
  31. )
  32. [3] => Array
  33. (
  34. [area_id] => 130204
  35. [area_pid] => 130200
  36. [area_name] => 古冶区
  37. [lng] => 118.458
  38. [lat] => 39.7199
  39. )
  40. [4] => Array
  41. (
  42. [area_id] => 130205
  43. [area_pid] => 130200
  44. [area_name] => 开平区
  45. [lng] => 118.262
  46. [lat] => 39.6713
  47. )
  48. [5] => Array
  49. (
  50. [area_id] => 130207
  51. [area_pid] => 130200
  52. [area_name] => 丰南区
  53. [lng] => 118.113
  54. [lat] => 39.5648
  55. )
  56. [6] => Array
  57. (
  58. [area_id] => 130208
  59. [area_pid] => 130200
  60. [area_name] => 丰润区
  61. [lng] => 118.13
  62. [lat] => 39.8244
  63. )
  64. [7] => Array
  65. (
  66. [area_id] => 130209
  67. [area_pid] => 130200
  68. [area_name] => 曹妃甸区
  69. [lng] => 118.46
  70. [lat] => 39.2731
  71. )
  72. [8] => Array
  73. (
  74. [area_id] => 130223
  75. [area_pid] => 130200
  76. [area_name] => 滦县
  77. [lng] => 118.703
  78. [lat] => 39.7406
  79. )
  80. [9] => Array
  81. (
  82. [area_id] => 130224
  83. [area_pid] => 130200
  84. [area_name] => 滦南县
  85. [lng] => 118.674
  86. [lat] => 39.5039
  87. )
  88. [10] => Array
  89. (
  90. [area_id] => 130225
  91. [area_pid] => 130200
  92. [area_name] => 乐亭县
  93. [lng] => 118.912
  94. [lat] => 39.4256
  95. )
  96. [11] => Array
  97. (
  98. [area_id] => 130227
  99. [area_pid] => 130200
  100. [area_name] => 迁西县
  101. [lng] => 118.316
  102. [lat] => 40.1459
  103. )
  104. [12] => Array
  105. (
  106. [area_id] => 130229
  107. [area_pid] => 130200
  108. [area_name] => 玉田县
  109. [lng] => 117.739
  110. [lat] => 39.9005
  111. )
  112. [13] => Array
  113. (
  114. [area_id] => 130281
  115. [area_pid] => 130200
  116. [area_name] => 遵化市
  117. [lng] => 117.964
  118. [lat] => 40.1874
  119. )
  120. [14] => Array
  121. (
  122. [area_id] => 130283
  123. [area_pid] => 130200
  124. [area_name] => 迁安市
  125. [lng] => 118.701
  126. [lat] => 39.9983
  127. )
  128. )
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议