PHP foreach数组循环
程序员文章站
2022-03-06 13:48:27
...
数组的数据
$arr=[
[
'area_id' => 110000,
'area_pid' => 0,
'area_name' => '北京',
'lng' => 116.405,
'lat' => 39.905,
'son' => [
[
'area_id' => 110100,
'area_pid' => 110000,
'area_name' => '北京市',
'lng' => 116.405,
'lat' => 39.905,
'son' => [
[
'area_id' => 110101,
'area_pid' => 110100,
'area_name' => '东城区',
'lng' => 116.41,
'lat' => 39.9316
],
[
'area_id' => 110102,
'area_pid' => 110100,
'area_name' => '西城区',
'lng' => 116.36,
'lat' => 39.9305
]
]
]
]
],
[
'area_id' => 120000,
'area_pid' => 0,
'area_name' => '天津',
'lng' => 117.19,
'lat' => 39.1256,
'son' => [
[
'area_id' => 120100,
'area_pid' => 120000,
'area_name' => '天津市',
'lng' => 117.19,
'lat' => 39.1256,
'son' => [
[
'area_id' => 120101,
'area_pid' => 120100,
'area_name' => '和平区',
'lng' => 117.215,
'lat' => 39.1172
],
[
'area_id' => 120102,
'area_pid' => 120100,
'area_name' => '河东区',
'lng' => 117.226,
'lat' => 39.1232
]
]
]
]
]
];
输出单个数据
print_r($arr[0]['son'][0]['area_name']); //输出北京市
循环出的数据
foreach ($arr as $v) {
echo '<ul>';
echo '<li>'.$v['area_id'].'-'.$v['area_pid'].'-'.$v['area_name'].'-'.$v['lng'].'-'.$v['lat'].'</li>';
foreach ($v['son'] as $v2) {
echo '<ul><li>'.$v2['area_id'].'-'.$v2['area_pid'].'-'.$v2['area_name'].'-'.$v2['lng'].'-'.$v2['lat'].'</li></ul>';
foreach ($v2['son'] as $v3) {
echo '<ul><ul><li>'.$v3['area_id'].'-'.$v3['area_pid'].'-'.$v3['area_name'].'-'.$v3['lng'].'-'.$v3['lat'].'</li></ul></ul>';
}
}
echo '</ul>';
echo '<hr>';
}
输出效果
上一篇: uniapp中单选按钮的实现
下一篇: 如何理解javascript原型链
推荐阅读