Blogger Information
Blog 38
fans 1
comment 0
visits 28757
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP数组 - 九期线上班
fkkf467
Original
613 people have browsed it

一. 创建数组

1. 创建空数组

  1. <?php
  2. $arr1 = array();
  3. var_dump($arr1);
  4. echo '<hr>';
  5. $arr2 = [];
  6. var_dump($arr2);
  7. ?>


2. 创建索引数组

  1. <?php
  2. $arr1 = array(
  3. '张三',
  4. '李四',
  5. '王五'
  6. );
  7. var_dump($arr1);
  8. echo '<hr>';
  9. print_r($arr1);
  10. echo '<hr>';
  11. $arr2 = [
  12. '苹果',
  13. '小米',
  14. '大米'
  15. ];
  16. var_dump($arr2);
  17. echo '<hr>';
  18. print_r($arr2);
  19. echo '<hr>';
  20. ?>


3. 创建关联数组

  1. <?php
  2. $arr1 = array(
  3. 'name' => '张三',
  4. 'sex' => '男',
  5. 'age' => 18
  6. );
  7. var_dump($arr1);
  8. echo '<hr>';
  9. $arr2 = [
  10. 'zs' => '张三',
  11. 'ls' => '李四',
  12. 'ww' => '王五'
  13. ];
  14. var_dump($arr2);
  15. echo '<hr>';
  16. ?>


4. 输出数组值

  1. <?php
  2. $arr1 = [
  3. '张三',
  4. '李四',
  5. '王五'
  6. ];
  7. echo $arr1[0] . ' ' . $arr1[1] . ' ' . $arr1[2];
  8. echo '<hr>';
  9. $arr2 = [
  10. 'ouyangke' => '欧阳克',
  11. 'zhulaoshi' => '朱老师',
  12. 'ximen' => '西门大官人'
  13. ];
  14. echo $arr2['ouyangke'];
  15. echo '<br>';
  16. echo $arr2['zhulaoshi'];
  17. echo '<br>';
  18. echo $arr2['ximen'];
  19. echo '<br>';
  20. ?>

二. 多维数组

1. 二维数组

  1. <?php
  2. $arr = [
  3. [
  4. 'name' => '张三',
  5. 'sex' => '男',
  6. 'age' => 18
  7. ],
  8. [
  9. 'name' => '李四',
  10. 'sex' => '男',
  11. 'age' => 19
  12. ],
  13. [
  14. 'name' => '王五',
  15. 'sex' => '男',
  16. 'age' => 21
  17. ]
  18. ];
  19. var_dump($arr);
  20. echo '<hr>';
  21. print_r($arr);
  22. ?>


2. 三维数组

  1. <?php
  2. $arr = [
  3. [
  4. 'name' => '张三',
  5. 'sex' => '男',
  6. 'age' => 18,
  7. 'job' => [
  8. 'java程序员',
  9. '画家'
  10. ]
  11. ],
  12. [
  13. 'name' => '李四',
  14. 'sex' => '男',
  15. 'age' => 19,
  16. 'job' => [
  17. 'php程序员',
  18. '架构师'
  19. ]
  20. ],
  21. [
  22. 'name' => '王五',
  23. 'sex' => '男',
  24. 'age' => 21,
  25. 'job' => [
  26. 'python程序员',
  27. '快递员'
  28. ]
  29. ]
  30. ];
  31. var_dump($arr);
  32. echo '<hr>';
  33. print_r($arr);
  34. ?>


3. 多维数组访问

  1. echo $arr[0]['name'] . '---' . $arr[0]['job'][0];
  2. echo '<hr>';
  3. echo $arr[1]['name'] . '---' . $arr[1]['job'][0];
  4. echo '<hr>';
  5. echo $arr[2]['name'] . '---' . $arr[2]['job'][0];
  6. echo '<hr>';

三. 多维数组循环

1. 二维数组循环

  1. <?php
  2. $arr = [
  3. [
  4. 'name' => '欧阳克',
  5. 'age' => 18
  6. ],
  7. [
  8. 'name' => '朱老师',
  9. 'age' => 20
  10. ],
  11. [
  12. 'name' => '西门大官人',
  13. 'age' => 24
  14. ]
  15. ];
  16. foreach ( $arr as $v ) {
  17. echo $v['name'] . '--' . $v['age'];
  18. echo '<br>';
  19. }
  20. echo '<hr>';
  21. foreach ( $arr as $v ) {
  22. foreach ( $v as $vv ) {
  23. echo $vv;
  24. echo '<br>';
  25. }
  26. }
  27. echo '<hr>';
  28. foreach ( $arr as $k=>$v ) {
  29. foreach ( $v as $kk=>$vv ) {
  30. echo $kk . ' ' . $vv;
  31. echo '<br>';
  32. }
  33. }
  34. ?>


2. 三维数组循环

  1. <?php
  2. $arr = [
  3. [
  4. 'name' => '张三',
  5. 'sex' => '男',
  6. 'age' => 18,
  7. 'job' => [
  8. 'java程序员',
  9. '画家'
  10. ]
  11. ],
  12. [
  13. 'name' => '李四',
  14. 'sex' => '男',
  15. 'age' => 19,
  16. 'job' => [
  17. 'php程序员',
  18. '架构师'
  19. ]
  20. ],
  21. [
  22. 'name' => '王五',
  23. 'sex' => '男',
  24. 'age' => 21,
  25. 'job' => [
  26. 'python程序员',
  27. '快递员'
  28. ]
  29. ]
  30. ];
  31. foreach ($arr as $value) {
  32. echo $value['name'];
  33. foreach ($value['job'] as $vv) {
  34. echo '---' . $vv;
  35. }
  36. echo '<br>';
  37. }
  38. echo '<hr>';
  39. foreach ( $arr as $value ) {
  40. foreach ( $value as $k=>$v ) {
  41. foreach ( $v as $vv ) {
  42. echo $vv;
  43. echo '<br>';
  44. }
  45. }
  46. echo '<hr>';
  47. }
  48. ?>


四. 总结

通过以上实例练习,学会了数组的相关知识,特别是对数组的循环有了较为深刻的理解。

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