1.循环省、市、区数组
<?php
// 导入省市区数组
include_once 'arr.php';
foreach ($arr as $keyProvince => $province) {
echo $province['area_name'] . '<br>';
foreach ($province['son'] as $keyCity => $city) {
echo '----' . $city['area_name'] . '<br>';
foreach ($city['son'] as $keyCity => $area) {
echo '----' . '----' . '----' . $area['area_name'] . '<br>';
}
}
}
结果:
2. 用 echo 输出单个值,用print_r()打印其中一个城市和区
// 用echo打印数组中的“北京”
echo $arr[0]['area_name'];
结果:北京
//用print_r()打印唐山市城市和区
print_r($arr[2]['son'][1]);
结果:
[area_id] => 130200
[area_pid] => 130000
[area_name] => 唐山市
[lng] => 118.175
[lat] => 39.6351
[son] => Array
(
[0] => Array
(
[area_id] => 130284
[area_pid] => 130200
[area_name] => 滦州市
[lng] =>
[lat] =>
)
[1] => Array
(
[area_id] => 130202
[area_pid] => 130200
[area_name] => 路南区
[lng] => 118.154
[lat] => 39.625
)
[2] => Array
(
[area_id] => 130203
[area_pid] => 130200
[area_name] => 路北区
[lng] => 118.201
[lat] => 39.6244
)
[3] => Array
(
[area_id] => 130204
[area_pid] => 130200
[area_name] => 古冶区
[lng] => 118.458
[lat] => 39.7199
)
[4] => Array
(
[area_id] => 130205
[area_pid] => 130200
[area_name] => 开平区
[lng] => 118.262
[lat] => 39.6713
)
[5] => Array
(
[area_id] => 130207
[area_pid] => 130200
[area_name] => 丰南区
[lng] => 118.113
[lat] => 39.5648
)
[6] => Array
(
[area_id] => 130208
[area_pid] => 130200
[area_name] => 丰润区
[lng] => 118.13
[lat] => 39.8244
)
[7] => Array
(
[area_id] => 130209
[area_pid] => 130200
[area_name] => 曹妃甸区
[lng] => 118.46
[lat] => 39.2731
)
[8] => Array
(
[area_id] => 130223
[area_pid] => 130200
[area_name] => 滦县
[lng] => 118.703
[lat] => 39.7406
)
[9] => Array
(
[area_id] => 130224
[area_pid] => 130200
[area_name] => 滦南县
[lng] => 118.674
[lat] => 39.5039
)
[10] => Array
(
[area_id] => 130225
[area_pid] => 130200
[area_name] => 乐亭县
[lng] => 118.912
[lat] => 39.4256
)
[11] => Array
(
[area_id] => 130227
[area_pid] => 130200
[area_name] => 迁西县
[lng] => 118.316
[lat] => 40.1459
)
[12] => Array
(
[area_id] => 130229
[area_pid] => 130200
[area_name] => 玉田县
[lng] => 117.739
[lat] => 39.9005
)
[13] => Array
(
[area_id] => 130281
[area_pid] => 130200
[area_name] => 遵化市
[lng] => 117.964
[lat] => 40.1874
)
[14] => Array
(
[area_id] => 130283
[area_pid] => 130200
[area_name] => 迁安市
[lng] => 118.701
[lat] => 39.9983
)
)