Home >Backend Development >PHP Tutorial >Several methods for traversing two-dimensional arrays in php

Several methods for traversing two-dimensional arrays in php

WBOY
WBOYOriginal
2016-07-25 08:58:483312browse
  1. /**

  2. * Traverse two-dimensional array
  3. * edit bbs.it-home.org
  4. */
  5. //for loop traversal
  6. $arr2=array(array("Zhang San","20","Male") ,array("李四","25","male"),array("王五","19","female"),array("Zhao Liu","25","female"));
  7. echo "< /tr>";
  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. NameAgeGender
    ";
  21. echo $arr2[$i][$j];
  22. echo "
  23. ";
  24. ?>

  25. //foreach traversal

  26. $arr = array('one'=>array('name'=> ;'张三','age'=>'23','sex'=>'male'),
  27. 'two'=>array('name'=>'李思','age' =>'43','sex'=>'female'),
  28. 'three'=>array('name'=>'王五','age'=>'32','sex '=>'Male'),
  29. 'four'=>array('name'=>'Zhao Liu','age'=>'12','sex'=>'Female')) ;

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

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

    ";

  34. //foreach traversal example 2

  35. $arr = array('one'=>array(' name'=>'Zhang San','age'=>'23','sex'=>'Male'),
  36. 'two'=>array('name'=>'李思' ,'age'=>'43','sex'=>'female'),
  37. 'three'=>array('name'=>'王五','age'=>'32 ','sex'=>'Male'),
  38. 'four'=>array('name'=>'Zhao Liu','age'=>'12','sex'=>' Female'));
  39. foreach($arr as $key=>$value){
  40. foreach($value as $key2=>$value2){
  41. echo $value2;
  42. }
  43. echo "
    " ;
  44. }
  45. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn