Blogger Information
Blog 31
fans 0
comment 0
visits 18236
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP第3课-PHP的数组创建和循环遍历-九期线上班
Content っ
Original
707 people have browsed it

1.创建数组

  1. //创建数组1
  2. $arr = array();
  3. //创建数组2
  4. $arr1 = [];

2.关联数组、索引数组、访问数组的数据

  1. //创建数组3
  2. $arr2 = array(
  3. 1,
  4. 2,
  5. 'Jason',
  6. '数学',
  7. 111.00,
  8. null
  9. );
  10. print_r( $arr2 );
  11. echo '<hr>';
  12. //创建数组4
  13. $arr3 = [
  14. 1333,
  15. 222,
  16. 'Jason+Jack',
  17. '数学+语言',
  18. 111.00,
  19. null
  20. ];
  21. print_r( $arr3 );
  22. echo '<hr>';
  23. //创建数组5
  24. $arr4 = [
  25. '230001' => '合肥市辖区',
  26. '230002' => '合肥庐阳区',
  27. '230004' => '合肥包河区',
  28. '230005' => '合肥政务区',
  29. ];
  30. print_r( $arr4 );
  31. echo '<hr>';
  32. //创建数组6
  33. $arr5 = [
  34. 'name' => 'Jason',
  35. 'age' => '15岁',
  36. 'sex' => '男',
  37. 'address' => '南昌市',
  38. ];
  39. print_r( $arr5['address'] );
  40. echo '<hr>';
  41. echo $arr5['name'];
  42. echo '<hr>';
  43. var_dump($arr5['age']);
  44. echo '<hr>';

3.二维数组、三维数组

  1. /******二维数组*****/
  2. $arr6 = [
  3. [ 'age' => '16',
  4. 'name' => 'jason',
  5. 'job' =>'学生'
  6. ],
  7. [ 'age' => '18',
  8. 'name' => '大狗',
  9. 'job' =>'饲养员'
  10. ],
  11. [ 'age' => '50',
  12. 'name' => 'jack',
  13. 'job' =>'老板'
  14. ],
  15. ];
  16. print_r( $arr6[1]['name'] );
  17. echo '<hr>';
  18. /******三维数组*****/
  19. $arr7 = [
  20. [ 'age' => '16',
  21. 'name' => 'jason',
  22. 'job' =>'学生',
  23. 'skill' => [
  24. 'html',
  25. '小程序',
  26. 'PHP'
  27. ]
  28. ],
  29. [ 'age' => '18',
  30. 'name' => '大狗',
  31. 'job' =>'饲养员',
  32. 'skill' => [
  33. 'html',
  34. 'css',
  35. 'js'
  36. ]
  37. ],
  38. [ 'age' => '50',
  39. 'name' => 'jack',
  40. 'job' => '老板',
  41. 'skill' => [
  42. 'Java',
  43. 'PHP',
  44. 'C#'
  45. ]
  46. ],
  47. ];
  48. print_r( $arr7[0]['skill'][1] );
  49. echo '<hr>';

5.二维数组循环,三维数组循环

  1. /*************数组循环***********/
  2. //前面是数组,后面就是循环遍历出来的str
  3. foreach ($arr5 as $str){
  4. echo $str;
  5. echo '<hr>';
  6. }
  7. //输出键值对两个值
  8. foreach ($arr5 as $key => $str){
  9. echo $key . '=======' . $str;
  10. echo '<hr>';
  11. }
  12. //二次循环遍历二维数组
  13. foreach ($arr6 as $arr){
  14. foreach ($arr as $str){
  15. echo $str;
  16. echo '<hr>';
  17. }
  18. }
  19. //二次循环遍历二维数组键值对
  20. foreach ($arr6 as $arr){
  21. foreach ($arr as $key => $str){
  22. echo $key . '=====' . $str;
  23. echo '<hr>';
  24. }
  25. }
  26. //一次循环遍历二维数组键值对,二次消耗内存
  27. foreach ($arr6 as $arr){
  28. echo $arr['name'];
  29. echo '<hr>';
  30. }
  31. //二次循环遍历三维数组
  32. foreach ($arr7 as $item) {
  33. echo '姓名'. '====' .$item['name'];
  34. echo '<hr>';
  35. echo '年龄'. '====' .$item['age'];
  36. echo '<hr>';
  37. echo '职务'. '====' .$item['job'];
  38. echo '<hr>';
  39. foreach ($item['skill'] as $str){
  40. echo '技能'. '====' .$str;
  41. echo '<br>';
  42. }
  43. echo '<hr>';
  44. }

运行效果

手抄代码



总结:
今天学习了数组创建和循环遍历,数组创建分为一维二维三维多维,越多越消耗内存,所以在使用中尽量越少越好,执行效率也比较快,循环遍历就是把数组中的值取出来。

Correcting teacher:查无此人查无此人

Correction status:qualified

Teacher's comments:完成的不错。
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post