欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-03-29 15:07:15
...
  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. ?>
复制代码