首页  >  文章  >  后端开发  >  php遍历二维数组的几种方法

php遍历二维数组的几种方法

WBOY
WBOY原创
2016-07-25 08:58:483283浏览
  1. /**

  2. * 遍历二维数组
  3. * edit bbs.it-home.org
  4. */
  5. //for循环遍历
  6. $arr2=array(array("张三","20","男"),array("李四","25","男"),array("王五","19","女"),array("赵六","25","女"));
  7. echo "& gt;";
  8. for($i=0;$iecho "
  9. ";
  10. for($j=0;$j echo "
  11. ";
  12. }
  13. echo "
  14. ";
  15. echo "
    ";
  16. }
  17. echo "
  18. 姓名 年龄性别
    ";
  19. echo $arr2[$i][$j];
  20. echo "
  21. ";
  22. ?>
  23. //foreach遍历

  24. $arr = array('one'=>array('name'=>'张三','age'=>'23','sex'=>'男'),
  25. 'two'=>array('name'=>'李四','age'=>'43','sex'=>'女'),
  26. 'three'=>array('name'=>'王五','age'=>'32','sex'=>'男'),
  27. 'four'=>array('name'=>'赵六','age'=>'12','sex'=>'女'));
  28. foreach($arr as $k=>$val){

  29. echo $val['name'].$val['age'].$val['sex']."
    ";
  30. }
  31. echo "

    ";

  32. //foreach遍历 例2

  33. $arr = array('one'=>array('name'=>'张三','age'=>'23','sex'=>'男'),
  34. 'two'=>array('name'=>'李四','age'=>'43','sex'=>'女'),
  35. 'three'=>array('name'=>'王五','age'=>'32','sex'=>'男'),
  36. 'four'=>array('name'=>'赵六','age'=>'12','sex'=>'女'));
  37. foreach($arr as $key=>$value){
  38. foreach($value as $key2=>$value2){
  39. echo $value2;
  40. }
  41. echo "
    ";
  42. }
  43. ?>
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn