首頁 >後端開發 >php教程 >php遍歷二維數組的幾種方法

php遍歷二維數組的幾種方法

WBOY
WBOY原創
2016-07-25 08:58:483314瀏覽
  1. /**

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

  25. //foreach遍历

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

  30. foreach($arr as $k=>$val){

  31. echo $val['name'].$val['age'].$val['sex']."
    ";
  32. }
  33. echo "

    ";

  34. //foreach遍历 例2

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


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn