Blogger Information
Blog 19
fans 0
comment 0
visits 13318
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
循环读取数组
王浩
Original
807 people have browsed it
  1. 作业内容:
  2. 1、把省市区 数组,循环一遍
  3. 2、用 echo 输出单个值,用print_r()打印其中一个城市和区
  4. 3、可以用2个循环,或者3个循环。 省市区数据,在课件里
  1. function fun1($arr){
  2. $html = "<table width=100%>";
  3. foreach($arr as $v1){
  4. foreach($v1['son'] as $v2){
  5. foreach($v2['son'] as $v3){
  6. $html .= "<tr><td>".$v1['area_id'] . "</td><td>" . $v1['area_name'] . "</td><td>";
  7. $html .= $v2['area_id'] . "</td><td>" . $v2['area_name'] . "</td><td>";
  8. $html .= $v3['area_id'] . "</td><td>" . $v3['area_name'] . "</td><td></tr>";
  9. }
  10. }
  11. }
  12. $html .= "</table>";
  13. return $html;
  14. }
  15. echo fun1($arr);
  1. // 闲得无聊,加个递归版本读取省、市、区
  2. function show($arr, $level = 0){
  3. foreach($arr as $k => $v){
  4. if(is_array($v['son'])){
  5. echo str_repeat("-", $level*4);
  6. echo $v['area_name']."<br/>";
  7. show($v['son'], $level+1);
  8. }else{
  9. echo str_repeat("-", $level*4);
  10. echo $v['area_name']."<br/>";
  11. }
  12. }
  13. }
  14. show($arr);
  1. // 显示效果
  2. 北京
  3. ----北京市
  4. --------东城区
  5. --------西城区
  6. --------朝阳区
  7. --------丰台区
  8. --------石景山区
  9. --------海淀区
  10. --------门头沟区
  11. --------房山区
  12. --------通州区
  13. --------顺义区
  14. --------昌平区
  15. --------大兴区
  16. --------怀柔区
  17. --------平谷区
  18. --------密云县
  19. --------延庆县
  20. 天津
  21. ----天津市
  22. --------和平区
  23. --------河东区
  24. --------河西区
  25. --------南开区
  26. --------河北区
  27. --------红桥区
  28. --------东丽区
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post