PHP简单实现二维数组赋值与遍历功能示例

这篇文章主要介绍了PHP简单实现二维数组赋值与遍历功能,涉及php数组的简单赋值、遍历、运算、读取等操作使用技巧,需要的朋友可以参考下,本文实例讲述了PHP简单实现二维数组赋值与遍历功能,分享给大家供大家参考,具体如下:

示例1:

  1. <?php
  2. $loptop1['lid'] = 1000;
  3. $loptop1['pic'] = 'img/1.png';
  4. $loptop1['title'] = 'L1';
  5. $loptop1['price'] = 5000;
  6. $loptop1['isOnSale'] = 1;
  7. $loptop1['shelfTime'] = 1234556;
  8. $loptop2['lid'] = 1001;
  9. $loptop2['pic'] = 'img/2.png';
  10. $loptop2['title'] = 'L2';
  11. $loptop2['price'] = 5000;
  12. $loptop2['isOnSale'] = 1;
  13. $loptop2['shelfTime'] = 123444456;
  14. $loptop3['lid'] = 1002;
  15. $loptop3['pic'] = 'img/3.png';
  16. $loptop3['title'] = 'L3';
  17. $loptop3['price'] = 5000;
  18. $loptop3['isOnSale'] = 1;
  19. $loptop3['shelfTime'] = 1243454556;
  20. $loptop4['lid'] = 1003;
  21. $loptop4['pic'] = 'img/4.png';
  22. $loptop4['title'] = 'L4';
  23. $loptop4['price'] = 5000;
  24. $loptop4['isOnSale'] = 1;
  25. $loptop4['shelfTime'] = 1234364556;
  26. $loptop[0] = $loptop1;
  27. $loptop[1] = $loptop2;
  28. $loptop[2] = $loptop3;
  29. $loptop[3] = $loptop4;
  30. for($i=0;$i<count($loptop);$i++){
  31. //echo "编号:$loptop[$i][lid]"; //错误
  32. //echo "编号:" . $loptop[$i]['lid']; //正确,但不推荐
  33. $tmp = $loptop[$i];
  34. echo "编号:$tmp[lid]<br/>";
  35. echo "图片:$tmp[pic]<br/>";
  36. echo "标题:$tmp[title]<br/>";
  37. echo "价格:$tmp[price]<br/>";
  38. echo "是否特价:$tmp[isOnSale]<br/>";
  39. echo "上架时间:" . date("Y-m-d H:i:s",$tmp['shelfTime']) . "<br/>";
  40. }
  41. ?>

运行结果:

  1. 编号:1000
  2. 图片:img/1.png
  3. 标题:L1
  4. 价格:5000
  5. 是否特价:1
  6. 上架时间:1970-01-15 06:55:56
  7. 编号:1001
  8. 图片:img/2.png
  9. 标题:L2
  10. 价格:5000
  11. 是否特价:1
  12. 上架时间:1973-11-29 18:07:36
  13. 编号:1002
  14. 图片:img/3.png
  15. 标题:L3
  16. 价格:5000
  17. 是否特价:1
  18. 上架时间:2009-05-27 20:02:36
  19. 编号:1003
  20. 图片:img/4.png
  21. 标题:L4
  22. 价格:5000
  23. 是否特价:1
  24. 上架时间:2009-02-11 15:02:36

示例2:

  1. <?php
  2. $stu1['sid'] = 1000;
  3. $stu1['userName'] = "abc1";
  4. $stu1['passWord'] = "123456";
  5. $stu1['email'] = "2109882885@qq.com";
  6. $stu1['tel'] = "15700769164";
  7. $stu1['headScu'] = "stu1.png";
  8. $stu1['sex'] = "M";
  9. $stu1['regTime'] = 1111223435;
  10. $stu1['isOnline'] = 1;
  11. $stu2['sid'] = 1001;
  12. $stu2['userName'] = "abc2";
  13. $stu2['passWord'] = "123456";
  14. $stu2['email'] = "2109882886@qq.com";
  15. $stu2['tel'] = "15700769165";
  16. $stu2['headScu'] = "stu2.png";
  17. $stu2['sex'] = "M";
  18. $stu2['regTime'] = 122435344;
  19. $stu2['isOnline'] = 1;
  20. $stu3['sid'] = 1002;
  21. $stu3['userName'] = "abc3";
  22. $stu3['passWord'] = "123456";
  23. $stu3['email'] = "2109882887@qq.com";
  24. $stu3['tel'] = "15700769166";
  25. $stu3['headScu'] = "stu3.png";
  26. $stu3['sex'] = "M";
  27. $stu3['regTime'] = 3463464567;
  28. $stu3['isOnline'] = 0;
  29. $stu4['sid'] = 1003;
  30. $stu4['userName'] = "abc4";
  31. $stu4['passWord'] = "123456";
  32. $stu4['email'] = "2109882888@qq.com";
  33. $stu4['tel'] = "15700769167";
  34. $stu4['headScu'] = "stu4.png";
  35. $stu4['sex'] = "F";
  36. $stu4['regTime'] = 235234534;
  37. $stu4['isOnline'] = 1;
  38. $stu = [$stu1,$stu2,$stu3,$stu4];
  39. for($i=0;$i<count($stu);$i++){
  40. $tmp = $stu[$i];
  41. echo "编号:$tmp[sid]<br/>";
  42. echo "用户名:$tmp[userName]<br/>";
  43. echo "密码:$tmp[passWord]<br/>";
  44. echo "邮箱:$tmp[email]<br/>";
  45. echo "手机:$tmp[tel]<br/>";
  46. echo "头像:$tmp[headScu]<br/>";
  47. if($tmp['sex'] == "M"){
  48. echo "性别:男<br/>";
  49. }
  50. if($tmp['sex'] == "F"){
  51. echo "性别:女<br/>";
  52. }
  53. echo "注册时间:" . date('Y-m-d H:i:s',$tmp['regTime']) . "<br/>";
  54. if($tmp['isOnline'] == 1){
  55. echo "状态:在线<br/>";
  56. }
  57. if($tmp['isOnline'] == 0){
  58. echo "状态:不在线<br/>";
  59. }
  60. }
  61. ?>

运行结果:

  1. 编号:1000
  2. 用户名:abc1
  3. 密码:123456
  4. 邮箱:2109882885@qq.com
  5. 手机:15700769164
  6. 头像:stu1.png
  7. 性别:男
  8. 注册时间:2005-03-19 09:10:35
  9. 状态:在线
  10. 编号:1001
  11. 用户名:abc2
  12. 密码:123456
  13. 邮箱:2109882886@qq.com
  14. 手机:15700769165
  15. 头像:stu2.png
  16. 性别:男
  17. 注册时间:1973-11-18 01:49:04
  18. 状态:在线
  19. 编号:1002
  20. 用户名:abc3
  21. 密码:123456
  22. 邮箱:2109882887@qq.com
  23. 手机:15700769166
  24. 头像:stu3.png
  25. 性别:男
  26. 注册时间:1943-08-27 03:01:11
  27. 状态:不在线
  28. 编号:1003
  29. 用户名:abc4
  30. 密码:123456
  31. 邮箱:2109882888@qq.com
  32. 手机:15700769167
  33. 头像:stu4.png
  34. 性别:女
  35. 注册时间:1977-06-15 14:55:34
  36. 状态:在线